SOLVED

Set local timezone using Velocity scripts

Go to solution
Anonymous
Not applicable

Hi all.

I am trying to use a script and Velocity to make the output of a date/time field more user friendly.

$date.format('MMMM dd, yyyy hh:mm a z', ${convert.parseDate(${lead.pexa_callbacktime}, 'yyyy-MM-dd')})

I am using the following Velocity script to achieve this, and so far I can successfully change the format, however it changes the timezone on me to GMT.  We are in Australia so need this to display AEST (or GMT+10).

The output i get is as follows.

Original Date/Time - 2016-09-14 11:00:00

Output after running through the above script - September 14, 2016 12:00 AM GMT

Any pointers on how to set the location or time zone in the output?  I am assuming this is picking up the timezone of Marketo's Apache server.

Thanks

Nick

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

When you first parse the Date from the string (inbound) include the timezone, and remember to include the time itself (you don't have it in your format pattern).

Instantiate the output timezone -- in the below snippet, in and out are the same -- and format the new string (outbound) with that timezone.

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

#set( $locale = $date.getLocale() )

#set( $myDate = $convert.parseDate($lead.pexa_callbacktime,'MM-dd-yy HH:mm:ss',$locale,$inTimeZone) )

${date.format('MMMM dd, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)}

View solution in original post

28 REPLIES 28
Mark_Wallace1
Level 4

Hello All

Just discovered this and found I can put a webinar into the contacts local timezone using the person time zone field value.  Fantastic.

Wanted to check is it possible to put the local campaign tokens into the velocity script.  Im currently including the webinar times by putting the webinar times into a field, when they are already in a local token.

If I could put the webinar start time token into the script that would make my day.

thanks for this

SanfordWhiteman
Level 10 - Community Moderator

What do you mean by "local campaign tokens"?  The built-in {{campaign.tokens}}?

SanfordWhiteman
Level 10 - Community Moderator

When you first parse the Date from the string (inbound) include the timezone, and remember to include the time itself (you don't have it in your format pattern).

Instantiate the output timezone -- in the below snippet, in and out are the same -- and format the new string (outbound) with that timezone.

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') )

#set( $locale = $date.getLocale() )

#set( $myDate = $convert.parseDate($lead.pexa_callbacktime,'MM-dd-yy HH:mm:ss',$locale,$inTimeZone) )

${date.format('MMMM dd, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)}

Victoria_Chu
Level 3

Hi Sanford Whiteman​ I'm looking to create an email velocity script that will display the stored date/time field in the U.S. pacific time zone.

Marketo support says date time field tokens populate with Central time regardless of the location the Marketo Instance is set. This is due to how field values are stored in Marketo.

Would I be able to follow the format of the solution you've provided and swap out any Australia/Sydney reference to US/Pacific? Thank you!

SanfordWhiteman
Level 10 - Community Moderator

This is due to how field values are stored in Marketo.

(It's actually not because of the way they're stored, but the behavior is known in any case.)

Would I be able to follow the format of the solution you've provided and swap out any Australia/Sydney reference to US/Pacific? Thank you!

Yes, the time zone name is

     America/Los_Angeles

Victoria_Chu
Level 3

Thank you Sanford Whiteman​ for the quick response!

This is the velocity script I'm now testing - the particular lead I'm previewing the email at has a create at date/time of Jan 8, 2019 10:22 AM. The velocity script is returning the value as Jan 8, 2019 12:22:00. What did I do wrong?

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/Los_Angeles') )  

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/Los_Angeles') )  

#set( $locale = $date.getLocale() )  

#set( $myDate = $convert.parseDate(${lead.Created_At},'yyyy-MM-dd HH:mm:ss',$locale,$inTimeZone) )  

${date.format('yyyy-MM-dd HH:mm:ss',$myDate,$locale,$outTimeZone)}

SanfordWhiteman
Level 10 - Community Moderator

Can I ask that you go back and highlight the code using the Advanced Editor's syntax highlighter? That makes it readable.

Also please take the curly braces out from ${lead.Created_At}. Use the simple notation $lead.Created_At. That isn't causing the problem but can cause other problems down the line.

When you simply print the String

$lead.Created_At

in Velocity, do you see

2019-01-08 10:22:00

?

Victoria_Chu
Level 3

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/Los_Angeles') )  

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/Los_Angeles') )  

#set( $locale = $date.getLocale() )  

#set( $myDate = $convert.parseDate($lead.Created_At,'yyyy-MM-dd HH:mm:ss',$locale,$inTimeZone) )  

${date.format('yyyy-MM-dd HH:mm:ss',$myDate,$locale,$outTimeZone)}

If I simply print the string

$lead.Created_At

In velocity I see

2019-01-08 12:22:00

SanfordWhiteman
Level 10 - Community Moderator

Sorry, the $inTimeZone should be America/Chicago, the $outTimeZone America/Los_Angeles.

Victoria_Chu
Level 3

Thank you it worked beautifully! Curious to your point about removing the {brackets} surrounding the fields. Why would it cause problems in the velocity script? We have that in place for all of our script and want to make sure they continue to function properly.

SanfordWhiteman
Level 10 - Community Moderator

Curious to your point about removing the {brackets} surrounding the fields. Why would it cause problems in the velocity script? We have that in place for all of our script and want to make sure they continue to function properly.

This blog post of mine should explain it: Velocity ${formal.notation} invites syntax errors: use it wisely

Victoria_Chu
Level 3

Update Sanford Whiteman​ - When I preview the email using a lead record, the script runs correct. However, in a real trigger/batch campaign, the script fails on the email, showing up blank. Can you think of why this would happen? Thanks.

SanfordWhiteman
Level 10 - Community Moderator

Showing up blank when the token exists is very rare. You should at least see an error dump.

Are you certain the token is in the folder hierarchy and the email is approved?

Victoria_Chu
Level 3

Yes and yes.

On Tue, Jan 8, 2019 at 5:42 PM Sanford Whiteman <marketingnation@marketo.com>

SanfordWhiteman
Level 10 - Community Moderator

And if you append another line to the token that just outputs some dummy text?

Victoria_Chu
Level 3

Sanford Whiteman​ I had put the script tokens in the program where the centralized alert email lives, not in the highest level of folder that houses all the program where the smart campaigns live. This explains perfectly why the preview worked but not the real send. Thanks for your help!

SanfordWhiteman
Level 10 - Community Moderator

Great!

Anonymous
Not applicable

Hi Sanford,

I'm looking to solve the same problem so our company can send a reminder email regarding a training. I'm not sure where I put this script. Is it in the email code header? Or do you add this to a snippet?

Also, I'm guessing I just need to adjust the '$lead.pexa_callbacktime' to the corresponding token field that I would have in this email 'company.Scheduled Training Date'.

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

#set( $locale = $date.getLocale() ) 

#set( $myDate = $convert.parseDate($company.Scheduled Training Date,'MM-dd-yy HH:mm:ss',$locale,$inTimeZone) ) 

${date.format('MMMM dd, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)} 

Cheers

Travis

SanfordWhiteman
Level 10 - Community Moderator

I'm looking to solve the same problem so our company can send a reminder email regarding a training. I'm not sure where I put this script. Is it in the email code header? Or do you add this to a snippet?

Neither: you create a new Email Script token and the code goes in the editor (left-hand side).

In the tree on the right-hand side, remember to check the boxes next to all fields you want to reference in the code -- in your case the Scheduled Training Date.  You should drag the field into the editor to find out the VTL (Velocity) name for the field.  It won't be the same as the normal token name because it can't have spaces. For example, {{lead.First Name}} as a normal token is $lead.FirstName in VTL.

Then the name of the Email Script token goes into the email using regular {{my.token}} syntax, for example {{my.velocity_training_date}} or whatever you call it.

Anonymous
Not applicable

I've set up the Email Script token as below. It re-arranges the date properly but still reverts to a different time zone.

Training date in salesforce:

24/05/2016 2:30PM

What shows up with the email token:

23 May, 2016 11:30 PM EST

#set( $inTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

#set( $outTimeZone = $date.getTimeZone().getTimeZone('Australia/Sydney') ) 

#set( $locale = $date.getLocale() ) 

#set( $myDate = $convert.parseDate($lead.Scheduled_Training_Date__c,'yy-MM-dd HH:mm',$locale,$inTimeZone) ) 

${date.format('dd MMMM, yyyy hh:mm a z',$myDate,$locale,$outTimeZone)} 

Any suggestions?