SOLVED

If/Then in Velocity Script

Go to solution
neelambakre
Level 3

Re: If/Then in Velocity Script

@SanfordWhiteman 

 

Thanks. Will keep this in mind.

Neelam Bakre
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

Well, you have a bunch of items I mentioned in my previous comment to think through before you finalize your custom object structure and the velocity script. But, checking for null values along with isEmpty() function should solve your initial query of why you're seeing additional rows in your email even though you don't have any data for those product fields (e.g., product, quantity4, etc.)

 

#if(cartAbandonment_cList.get(0).prod1 && !cartAbandonment_cList.get(0).prod1.isEmpty())
##Display something
#end

 

The above is just sample script to show how'd set your if condition so you take care of both null and empty "" values. Also, ideally, you should sort your CO list in the desired order before accessing the objects/loop through the list to pick the correct CO record based on a field. In general, accessing any random custom object record w/o sorting the custom object list isn't a good idea.

 

SanfordWhiteman
Level 10 - Community Moderator

Re: If/Then in Velocity Script

I think we should steer OP away from ever using .get(0) because its meaning isn’t understood by inexperienced devs.

 

Always iterate over the list, just stop after certain number of items.

 

i.e.

#set( $list = [1,56,12,34] )
#set( $sortedList = $sorter.sort( $list ) )
#set( $itemsToDisplay = 2 )
#foreach( $item in $sortedList.subList(0, $itemsToDisplay) )
$item
#end

 

 

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

Yeah- that's why I mentioned that the ideal way is to sort first, and then reference the record, but I do also agree and see people just copying script w/o reading/understanding causing all sorts of issues/confusion later on. Thank you, Sandy!

 

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

Gotcha, for such use cases the integration directly creates CO records in Marketo via API, as most businesses like to send the cart abandonment email within a few hours. Anyways, you can use the below piece of script to sort the CO records in the descending order of their createdAt datetime field. Once the list is sorted, you can reference it (using the sorted list variable, and not the CO List) in your script to show the data in the email.

 

#set( $sortedList = $sorter.sort($customObjList,"createdAt:desc") )  
The most recently updated is ${sortedList[0]}

 

Additionally, you say you'll have separate fields for storing each product, I find that difficult to scale. Do you have an enforced upper limit on the number of products a person could add or something like you'd display max n products of all in the abandoned cart email? If not, the number of fields on the CO could easily be a bottleneck. I'd rather advise you to coalesce data into a JSON format and write it to a text field on CO (create an additional JSON text field if you feel the text area field's limit of 30k characters might not be enough).

 

Lastly, if you're sending emails via a trigger-based campaign using Added to <custom object name> trigger, then you can also use the $TriggerObject to reference the CO that triggered the campaign to add data in the email. Also, could you let us know the output of $cartAbandonment_cList for one of the person records where you don't have all the products filled in? Feel free to mask any sensitive info. It'd be nice if we could see your full code as well. Thanks!

 

neelambakre
Level 3

Re: If/Then in Velocity Script

At present, there is no definitive upper limit set for the number of products, it is still under discussion from the client's side. For testing purposes, I have assumed a limit of 6 products.

 

I am providing you with the sample data I have created, including the fields expected from the client.

 

neelambakre_0-1687418988829.png

 

Field Description -

 

DataLoad : The date on which data is loaded in file and shared with us.

Quantity : No. of products in cart

CartVintage : No. of days  a customer adds items to the cart until the completion of the purchase

CartId : Its unique id per cartitem

Prod1-Prod6 : Name of products

Quant1-Quant6 : Quantity of associated product

Amt1 - Amt6 : Amount of Associated product

 

Thanks in advance.

Neelam Bakre
SanfordWhiteman
Level 10 - Community Moderator

Re: If/Then in Velocity Script

Again, a screenshot of a sheet is not useful. If you are supplying data, make it actual copy-and-pastable text.

 

But the contents of your CSV aren’t sufficient in any case. Please supply the output of the entire ${list} variable from Velocity. This is the only way we can see the Velocity names of the properties. The sheet will not show that.

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

Yeah- agreed with Sandy here. We need to see the actual data, i.e., the actual output of cartAbandonment_cList when you preview the email for a person record. Additionally, I think you should first fix your custom object definition and then start with the velocity logic, as the logic would change per you structure your CO. E.g., you can have seperate fields for each product and it's properties (amount, quantity, name, etc.), or you could also have a single text area field with all the product details coalesced in the JSON format - your script would be different in both the case.

 

neelambakre
Level 3

Re: If/Then in Velocity Script

Hi @Darshil_Shah1 @SanfordWhiteman ,

 

Apologies for delayed response.

 

The following is dummy data from a custom object. It contains 4 records with dummy information. The actual data will be expected in the same format.

 

I hope I hv provided you the data in requested format.

 

 

[{prod4=null, prod2=B, amnt3=null, quant4=null, prod3=null, dateLoad=2023-06-27 00:00:00, email=xyz@abc.com, cartCreated=2023-12-04 20:35:00, quant3=null, amnt2=5000, name=Neelam, quant2=400, cartUpdated=2023-06-17 00:00:00, amnt1=200, updatedAt=2023-06-27 09:15:51, prod1=A, noOfProducts=2, quant1=200, amnt4=null, createdAt=2023-06-27 09:15:51, customerID=Dummy123}]

[{prod4=null, prod2=B, amnt3=8975, quant4=Y, prod3=X, dateLoad=2023-06-27 00:00:00, email=abc@xyz.com, cartCreated=2023-03-27 00:00:00, quant3=47, amnt2=5000, name=Saurabh T, quant2=671, cartUpdated=2023-06-17 00:00:00, amnt1=200, updatedAt=2023-06-27 09:15:51, prod1=A, noOfProducts=4, quant1=1234, amnt4=7890, createdAt=2023-06-27 09:15:51, customerID=Dummy125}]

[{prod4=null, prod2=null, amnt3=null, quant4=null, prod3=null, dateLoad=2023-06-27 00:00:00, email=xyzabc@abc.com, cartCreated=2023-05-04 13:34:00, quant3=null, amnt2=null, name=Tirth, quant2=null, cartUpdated=2023-05-04 13:34:00, amnt1=7890, updatedAt=2023-06-27 09:15:51, prod1=u, noOfProducts=1, quant1=34, amnt4=null, createdAt=2023-06-27 09:15:51, customerID=Dummy130}]

[{prod4=null, prod2=Z, amnt3=null, quant4=null, prod3=null, dateLoad=2023-06-27 00:00:00, email=abc@xyza.com, cartCreated=2023-05-04 13:34:00, quant3=null, amnt2=7890, name=Himanshi, quant2=657, cartUpdated=2023-05-04 13:34:00, amnt1=8975, updatedAt=2023-06-27 09:15:51, prod1=Y, noOfProducts=2, quant1=789, amnt4=null, createdAt=2023-06-27 09:15:51, customerID=Dummy127}]

 

 

  

Thanks and Regards,

Neelam Bakre
neelambakre
Level 3

Re: If/Then in Velocity Script

Hi @Darshil_Shah1 @SanfordWhiteman ,

 

Hope you got chance to see my last comment.

 

Thanks

Neelam Bakre