SOLVED

If/Then in Velocity Script

Go to solution
rmoravick
Level 2

If/Then in Velocity Script

I am creating a Velocity Script for an email and would like to only show the script to users who have the custom objects in the script available. I am new to scripting but is there an if/then statement that can be used to say "if this custom object doesn't exist for a user, do not show the script"? Thank you!

2 ACCEPTED SOLUTIONS

Accepted Solutions
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.

 

View solution in original post

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

 

 

View solution in original post

24 REPLIES 24
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

You can use the size() function to determine if there are CO records or not for an associated person. The script would go something like below -

#if($test_CBC_cList.size()>0) //Replace "test_CBC_c" with the API name of your CO
Non-Zero CO records
#else
Zero CO records
#end

Hope this helps.

 

SanfordWhiteman
Level 10 - Community Moderator

Re: If/Then in Velocity Script

I like to use isEmpty():

#if( !$myList.isEmpty() )
## the list exists and has > 0 items
#end

List.isEmpty() is overloaded and is the same as checking the size.

 

If the list dos not exist at all,  size() will throw a fatal error; in the same situation, isEmpty() will fail silently, though it’s impossible to say which option is universally better. You can explicitly check the existence of the list first:

#if( $myList && !$myList.isEmpty() )
## the list exists and has > 0 items
#end

 

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

Thank you, Sandy. I liked the idea of checking if the list exists or not in the first place!

 

rmoravick
Level 2

Re: If/Then in Velocity Script

Thank you both for your input. This is the particular use case I'm trying to solve for

#if ($contributionHistory_cList.isEmpty())
Dont show anything

#else

Last Contribution Date: ${contributionHistory_cList.get(0).date}
Last Contribution Amount: ${contributionHistory_cList.get(0).contribution}
Manulife ID: ${contributionHistory_cList.get(0).manulifeID}
Plan Name: ${contributionHistory_cList.get(0).planName}

#end

If the Contribution List is Empty, it shouldn't display anything, but im still seeing the script conditions appear when previewing the email. 

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

That's what Sandy mentioned in his comment above. In case the list does not exist at all isEmpty() will fail silently. To avoid that, you could check the existence of the list first. Also, since you're not sorting the list, FYR the 0th index would return the CO record that was last created, as records by default are sorted by create date in descending order.

 

Try the below script instead - 

 

#if($contributionHistory_cList && !$contributionHistory_cList.isEmpty())
Last Contribution Date: ${contributionHistory_cList.get(0).date}
Last Contribution Amount: ${contributionHistory_cList.get(0).contribution}
Manulife ID: ${contributionHistory_cList.get(0).manulifeID}
Plan Name: ${contributionHistory_cList.get(0).planName}
#end

 

Hope this is helpful! Let us know how it goes.

neelambakre
Level 3

Re: If/Then in Velocity Script

Hi All ,

 

I am unfamiliar with velocity script, but I attempted to write the following script. Its purpose is to hide a table row if the product field value is empty, and the row should only be visible if the same field contains any value. Here I have created 6 tokens for 6 products. If a customer has purchased two products, only two product rows should be visible in the emailer, while the rest of the rows should not appear. Likewise, if the customer has purchased three products, three rows should be visible. However, the current implementation of the script is displaying all the rows. 

 

Please point out where I may have gone wrong.

 

Script :

neelambakre_1-1687352559069.png

 

 
Mailer Screenshot :
 
neelambakre_0-1687352358006.png

 

Thanks & Regards,
Neelam B.
 
Neelam Bakre
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: If/Then in Velocity Script

Well, do you have a different custom object record for each product? And, how do you determine whether the custom object record is the one you should consider in case the person record already has custom object records from the previous cart abandonments? One idea is to sort the custom object list based on the created date in descending order (even though by default the COs are sorted based on ascending order of the created date, but just in case, it's always good to sort it before you reference them), and reference the records with the same date for populating data in the email. Lastly, you don't want to hide rows (as you have mentioned in your comment), you should create and show rows for the records you want to display in the email.

 

neelambakre
Level 3

Re: If/Then in Velocity Script

Hi @Darshil_Shah1 ,

 

At present, the client is not sharing real-time data with us, necessitating a manual data processing approach. We will manually import the data into a custom object, there is only single custom object containing all the columns. If the customer has purchased multiple products, such as three, there will be separate columns for each product (Prod1, Prod2, Prod3), as well as separate columns for the corresponding amounts and quantities. There will be only single record present for 1 customer. The client will provide us with this data on a weekly basis, and our communication will be based on that.

 

Could you assist me in understanding how to sort the custom object before referencing it ?

What script-level modifications needs to be done to display rows based on the products associated with each customer ?

 

Thanks in advance.

Neelam Bakre
SanfordWhiteman
Level 10 - Community Moderator

Re: If/Then in Velocity Script

Please start by providing actual code, not a screenshot. A screenshot is not useful for troubleshooting.

 

When pasting code, highlight it as Java (closest we have to Velocity syntax) using the Insert/Edit Code Sample button:

SanfordWhiteman_0-1687414088834.png