2 Replies Latest reply on May 30, 2017 10:09 AM by Dakota Lewis

    Using Velocity Scripting with multiple instances of one variable

    Dakota Lewis



      We are trying to use velocity scripting in a token in order to generate emails for abandoned shopping carts.

      The below text is part of a token we can insert into emails, and the Shopping Cart is a custom object. This code works in our tests for giving the information for one product. However, we haven't been able to alter it to make it display information for multiple products. Even if we have multiple kinds of products loaded into the lead, we can't seem to figure out how to alter the script to reflect the below information for each product in the cart.



      You left the following item in your shopping cart. Don't forget about it!





                      <br> Price: ${esc.d}${shoppingCart_cList.get(0).itemPrice}


                      <br> Quantity: ${shoppingCart_cList.get(0).itemQuantity}


                      <br> Line Amount: ${esc.d}${shoppingCart_cList.get(0).extendedLineAmount}



      Do we need to create multiple shopping cart/item variables to make this happen? For instance, having itemDescription2, itemImageUrl2, etc.? I tried experimenting with if statements and a foreach, but I'm a complete beginner, so even just some hints would be very helpful. I can't seem to find any research online to help me understand how I need to alter our script.

        • Re: Using Velocity Scripting with multiple instances of one variable
          Sanford Whiteman

          Doesn't seem like a Velocity issue really.


          ${shoppingCart_cList.get(0) (it's more concise to use ${shoppingCart_cList[0]} by the way) is the first item in the list of shoppingCart objects. If that object is defined as having a single string property itemDescription, for example, then obviously you're only putting one item in there. There's nothing else for Velocity to access.


          Rather than creating a bunch of additional properties like itemDescription2 (which will fail as soon as you have over n items and is painful to maintain) I would use a single text field, stored as a JSON array of objects, to represent the cart. Each item is an object; its description, URL, price, quantity, extended total are all properties of the object.


          Or you could create item1, item2, item3 as separate text fields if you want (each holding a JSON object) bearing in mind the maintenance concern above.

          2 of 2 people found this helpful