SOLVED

Default Value for Velocity Token

Go to solution

Default Value for Velocity Token

I've been working on script tokens to beef up our member communications so that on our newsletters we can pass in their next event and the type and date for all trainings some is scheduled for (this information is stored in Custom Objects). 

We've used scripts like this in the past so I have the basis working but I'm running into an issue for the upcoming trainings token. The goals is to list out all the trainings and what day they are scheduled for, we have that part working, what I'm struggling with is if they are not scheduled for any trainings, they would like to display a message about how the customer should schedule. Here is the currently token without a default message:

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $Calendar = $date.getCalendar() )
#foreach( $event in $training_Record_cList )
#if( $event.trainingStatus.equalsIgnoreCase("confirmed") )
#set( $latestEventDate = $event.trainingScheduledStartDate )
#set( $origDate = $convert.parseDate($latestEventDate,$ISO8601DateOnly,$defaultLocale,$defaultTimeZone) )
#set( $formattedDate = $date.format('full_date',$origDate,$defaultLocale,$defaultTimeZone) )
#set ($trainingType = $event.trainingType)
#set ($trainingTypeSimple = $trainingType.replaceAll("\s*\(.*\)\s*$", ""))
<ul style="margin-bottom: 0px;">
<li style="margin-bottom: 0px;"><span style="font-style: italic;">$!trainingTypeSimple</span> on $!formattedDate</li>
</ul>
#end
#end

I originally tried to tried to do an else statement at the end but it ended up loading in when people had trainings and repeating a bunch of times (I'm guessing because of the #foreach). The other idea I had would be to look for them to not have a record where the training status was confirmed but I ran into a road block with that and could not figure out how to get it to work. 

Any help would be greatly appreciated! 

1 ACCEPTED SOLUTION

Accepted Solutions
Level 5

Re: Default Value for Velocity Token

Hello there are a few ways to do what you would like. 

My suggestion is based on providing some new options to get even more out of Velocity:

Basically, you could set a default value and then override it if they have a confirmed training session. This technique would let you have a dynamic message or one in certain scenarios. 

On line 11 there's a default set. Note: you can set a variable to a block of HTML if encapsulated in single quotes. 

##date defaults
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $Calendar = $date.getCalendar() )
##other detaults
#set( $scheduleMessage = '<span style="font-size:14px;"><p>HTML for default message here</p></span>' )
#foreach( $event in $training_Record_cList )
#if( $event.trainingStatus.equalsIgnoreCase("confirmed") )
#set( $latestEventDate = $event.trainingScheduledStartDate )
#set( $origDate = $convert.parseDate($latestEventDate,$ISO8601DateOnly,$defaultLocale,$defaultTimeZone) )
#set( $formattedDate = $date.format('full_date',$origDate,$defaultLocale,$defaultTimeZone) )
#set( $trainingType = $event.trainingType )
#set( $trainingTypeSimple = $trainingType.replaceAll("\s*\(.*\)\s*$", "") )
<ul style="margin-bottom: 0px;">
<li style="margin-bottom: 0px;"><span style="font-style: italic;">$!trainingTypeSimple</span> on $!formattedDate</li>
</ul>
#end
#end
##override schedule message
#if( $trainingTypeSimple )
#set( $scheduleMessage = '<span style="font-size:14px;"><p>Hope to see you soon!</p></span>' )
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here's the tricky/cool part: 

You can make a 2nd email script token to output your HTML where you would like your message to appear. 

*Beware, it needs to be underneath the main token to work.

For example, make an email script token: 
{{my.scheduleMessage}}

within {{my.scheduleMessage}} enter your velocity variable:
$scheduleMessage

Now you can edit your email in the UI and place the {{my.scheduleMessage}} token wherever you want the default message HTML to appear. **Again, it needs to be underneath the main token setting the variable to work.

For complicated builds I'll make a huge mega-token that sets all my variables and put it at the top of the email or even as the subject line.  Then I'll have a set of 'simple email script' tokens that are just 1 variable each. 

It seems to make the scripting a little easier having it all in one place and it's also easier to make changes or do highly dynamic emails. 

Disclaimer: haven't tested the above code so be sure to do so if you go this direction. 

hope it helps! 

I frequent: https://blog.teknkl.com/tag/velocity/ quite often 

View solution in original post

3 REPLIES 3
Level 5

Re: Default Value for Velocity Token

Hello there are a few ways to do what you would like. 

My suggestion is based on providing some new options to get even more out of Velocity:

Basically, you could set a default value and then override it if they have a confirmed training session. This technique would let you have a dynamic message or one in certain scenarios. 

On line 11 there's a default set. Note: you can set a variable to a block of HTML if encapsulated in single quotes. 

##date defaults
#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $Calendar = $date.getCalendar() )
##other detaults
#set( $scheduleMessage = '<span style="font-size:14px;"><p>HTML for default message here</p></span>' )
#foreach( $event in $training_Record_cList )
#if( $event.trainingStatus.equalsIgnoreCase("confirmed") )
#set( $latestEventDate = $event.trainingScheduledStartDate )
#set( $origDate = $convert.parseDate($latestEventDate,$ISO8601DateOnly,$defaultLocale,$defaultTimeZone) )
#set( $formattedDate = $date.format('full_date',$origDate,$defaultLocale,$defaultTimeZone) )
#set( $trainingType = $event.trainingType )
#set( $trainingTypeSimple = $trainingType.replaceAll("\s*\(.*\)\s*$", "") )
<ul style="margin-bottom: 0px;">
<li style="margin-bottom: 0px;"><span style="font-style: italic;">$!trainingTypeSimple</span> on $!formattedDate</li>
</ul>
#end
#end
##override schedule message
#if( $trainingTypeSimple )
#set( $scheduleMessage = '<span style="font-size:14px;"><p>Hope to see you soon!</p></span>' )
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Here's the tricky/cool part: 

You can make a 2nd email script token to output your HTML where you would like your message to appear. 

*Beware, it needs to be underneath the main token to work.

For example, make an email script token: 
{{my.scheduleMessage}}

within {{my.scheduleMessage}} enter your velocity variable:
$scheduleMessage

Now you can edit your email in the UI and place the {{my.scheduleMessage}} token wherever you want the default message HTML to appear. **Again, it needs to be underneath the main token setting the variable to work.

For complicated builds I'll make a huge mega-token that sets all my variables and put it at the top of the email or even as the subject line.  Then I'll have a set of 'simple email script' tokens that are just 1 variable each. 

It seems to make the scripting a little easier having it all in one place and it's also easier to make changes or do highly dynamic emails. 

Disclaimer: haven't tested the above code so be sure to do so if you go this direction. 

hope it helps! 

I frequent: https://blog.teknkl.com/tag/velocity/ quite often 

View solution in original post

Re: Default Value for Velocity Token

Hey Mark,

Thanks for the suggestion it was very helpful! The code above didn't work outright so I edited it a bit and wanted to see if you could give it a once over to make sure it looks good. I think the reason it didn't work is I only needed the message to replace if they don't have any trainings scheduled. So I set it to look for trainingTypeSimple to be null and then replace the scheduled message. Here's my code: 

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $Calendar = $date.getCalendar() )
#set( $scheduleMessage = '<span style="font-size:14px;"><p>To view ALL training day details, visit simembers.com/oss!</p></span>' )
#foreach( $event in $training_Record_cList )
#if( $event.trainingStatus.equalsIgnoreCase("confirmed") )
#set( $latestEventDate = $event.trainingScheduledStartDate )
#set( $origDate = $convert.parseDate($latestEventDate,$ISO8601DateOnly,$defaultLocale,$defaultTimeZone) )
#set( $formattedDate = $date.format('full_date',$origDate,$defaultLocale,$defaultTimeZone) )
#set( $trainingType = $event.trainingType )
#set( $trainingTypeSimple = $trainingType.replaceAll("\s*\(.*\)\s*$", "") )
<ul style="margin-bottom: 0px;">
<li style="margin-bottom: 0px;"><span style="font-style: italic;">$!trainingTypeSimple</span> on $!formattedDate</li>
</ul>
#end
#end
#if( "$!trainingTypeSimple" == "" )
#set( $scheduleMessage = '<span style="font-size:14px;"><p>You have no upcoming trainings scheduled! Please visit simembers.com or call our Customer Loyalty Team to schedule your next training.</p></span>' )
#end

Then I used you suggestion and made a separate token to put for scheduled message and just placed that in the email where it would need to populate.

It all seems to be working but wanted to make sure there weren't any glaring error first. And I am 100% with you on using https://blog.teknkl.com/tag/velocity/, that and his community posts are the only reason my token was at the point it was.

Thanks!

Level 10 - Community Moderator

Re: Default Value for Velocity Token

Instead of 

#if( "$!trainingTypeSimple" == "" )  ‍‍

you should use 

#if( $trainingTypeSimple.isEmpty() )

If $trainingTypeSimple may be null at that point, use

#if( $display.alt($trainingTypeSimple,"").isEmpty() )

In general, prefer .equals() and .isEmpty() to the == operator. == can lead to nasty surprises.