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!
Solved! Go to Solution.
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.
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
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
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!
Thank you, Sandy. I liked the idea of checking if the list exists or not in the first place!