SOLVED

Re: If/Then in Velocity Script

Go to solution
rmoravick
Level 2

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
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

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

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
SanfordWhiteman
Level 10 - Community Moderator

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

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!

 

neelambakre
Level 3

@SanfordWhiteman 

 

Thanks. Will keep this in mind.

Neelam Bakre
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

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