SOLVED

Re: Date Format Velocity Script

Go to solution
Jim_Clarke
Level 2

Hello,

It's been a while since I formatted dates with Velocity, I'm struggling with the DateTool methods and was wondering if someone had an example.

Here's my snippet:

##sets timestamp variable

#set ( $NextAppointment = ${OpportunityList.get(0).NextPhoneCallAppointment__c})

##This displays the timestamp unformatted

$NextAppointment

##This should display the timestamp formatted

$date.format('medium',$NextAppointment)

My sample emails renders like this, clearly the method is working:

2015-06-18 17:30:00 $date.format('medium',$NextAppointment)

I was using this example to get to this state:

DateTool (VelocityTools 2.0-beta4 Documentation)

Any help would be greatly appreciated as to what I might be doing incorrectly on calling the method.

Tags (2)
1 ACCEPTED SOLUTION
Anonymous
Not applicable

So with "medium" you're wanting something like Jun 18, 2015 5:30 PM?

If that's the case, something like this might work. I tested it with a similar field but don't know what format you're date was originally in:

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${OpportunityList.get(0).NextPhoneCallAppointment__c}), 'yyyy-MM-dd HH:mm:ss')})

View solution in original post

19 REPLIES 19
Phillip_Wild
Level 10

Hey team

I'm trying something similar, but when I use the code given to Jim it's not working for me. Here's my code:

#set ($listlength = ${abandonedCart_cList.size()})

#set( $lastIndex = $math.sub($listlength,1) )

#set($str = $convert.parseDate(${abandonedCart_cList.get($lastIndex).serviceStartDate}, "yyyy-MM-dd HH:mm:ss")) 

$date.format('medium',$str) 

I only get the output $date.format('medium',$str)  instead of the formatted date. Why? Those last two lines are identical to what seemed to be working above, just with my variables substituted in.

Any ideas?

SanfordWhiteman
Level 10 - Community Moderator

Look for any of my posts where I show data parsing + display.

Make sure the date you're parsing really is in that string format, and debug each variable as you build it up. Plus wouldn't call the date variable $str as it's a Java Date at that point, not a String.
Anonymous
Not applicable

So with "medium" you're wanting something like Jun 18, 2015 5:30 PM?

If that's the case, something like this might work. I tested it with a similar field but don't know what format you're date was originally in:

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${OpportunityList.get(0).NextPhoneCallAppointment__c}), 'yyyy-MM-dd HH:mm:ss')})

Tim_Langlois1
Level 4

Super helfpul Ryan, just ran across this and it solved my email script token date formatting issue.

Here's my entire script where the formatting is being used:

#if (${OpportunityList.get(0).New_Appoinment_c__c} != "")

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${OpportunityList.get(0).New_Appoinment_c__c}, 'yyyy-MM-dd HH:mm:ss')}) 

#else

  Please contact us to confirm your appointment.

#end

And this will display/print the date and time text from the New Appointment field on the opportunity in an email subject or body like this:

Aug 07, 2015 09:25 AM

Anonymous
Not applicable

We are getting a strange error on this....the script works fine in samples, but when actually sending this as an alert it breaks down (even though we are positive these leads have values in their lead level field). The campaign can only be run when those exact values get populated, so I'm not sure why this is happening...

#if (${lead.myvu_enrolled_date} != "")

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${lead.myvu_enrolled_date}, 'yyyy-MM-dd HH:mm:ss')})

#else

  Enrollment Date Unavailable

#end

Anonymous
Not applicable

When you say it 'breaks down', what exactly happens?

Do you get error merged in the email?

or

You get 'Enrollment Date Unavailable' all the time?

or

get blank value merged in the email?

Rajesh

Anonymous
Not applicable

Sorry should have been a bit more descriptive there! It returns the following:

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${lead.myvu_enrolled_date}, 'yyyy-MM-dd HH:mm:ss')})

We actually use the same script elsewhere, only with an opportunity level field instead of a lead level one, and it works fine sent as alerts. I am wondering if this script using a lead level field may be causing issues...

We use the script below for a different campaign, and although it is fairly different, I feel like the core operation is the same. This one has worked well so far.

#foreach($opportunity in $OpportunityList)

  #if($opportunity.o_opportunity_type == "Home Loan" && $opportunity.o_pal_download_amount && $opportunity.Name == $lead.crmidentifier)

<p class="copy" style="margin: 0px;">$date.format('MMM dd, yyyy hh:mm aa',${convert.parseDate(${opportunity.o_pal_download_time},'yyyy-MM-dd HH:mm:ss')})</p>

  #break

    #end 

#end

Anonymous
Not applicable

Your error output looks like what it does when you forget to check the box next to the field in the script window. Are you sure the "lead.myvu_enrolled_date" field is checked?

Anonymous
Not applicable

Ryan,

How would you go about adding a week to a date stored in a variable?

Thanks

Anonymous
Not applicable

Hi Reed... I haven't quite mastered adding a date to a specific field... but I have come up with a way around it. Not sure if this will work in your situation but:

If my smart list is triggering off of a SFDC custom field "Purchase Date" being 5 days ago, I can use "Today" as the field to add to. So say I want to place a date in an email that is 20 days from the date they purchased, but I want to send the email 5 days after they purchased, I can just add 15 days to today's date and place it in the email with this scripted token:

#set($x = $date.calendar)

$x.add(5,15)

$date.format('EEEE, MMMM dd, yyyy',$x)

I'm determined to figure out how to add days to existing date fields.

Anonymous
Not applicable

Okay, well after trying the same thing for weeks, it finally worked:

#set($lead.ProspectDate = $date.calendar)

$lead.ProspectDate.add(5,36)

$date.format('EEEE, MMMM dd, yyyy',$lead.ProspectDate)

"36" is where you put how many days you want to add to the date. "5" represents that are you adding calendar days. Good luck!

Anonymous
Not applicable

It is checked (that is always frustrating to test something for an extended period of time only to realize the box was unchecked!). I have contacted support about this problem - this token in particular only works in samples, never as an alert.

Dan_Askin
Level 4

(Brand) new to email scripting and having a similar issue. We've got appointment times (80+) added as attributes within a datetime field. Format within the smart list view is May 7, 2016 3:00 PM. Thought it would be as easy adding a lead level token for that attribute -- {{lead.saintsseatlocation:default=]] -- but it's rendering as W3C standard for datetime, 2016-04-29 11:00:00.

I'm a little confused about what goes in email script token field and what goes in the actual email html.

Learning Java basics in an hour is probably not the best route.

We're going to re-upload the attribute as a text field in CRM, but I'd really like to solve the riddle regardless.

Anonymous
Not applicable

Hi Dan,

Something like this should work in the email script itself, but I didn't test it (I just plugged it into the script that I already had working). Always make sure the box next to the field you are using is checked within the script editor. Then, in your email, you just need the name of the email script token, like {{my.testfield}} or whatever you call it.

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${lead.saintseatlocation}), 'yyyy-MM-dd HH:mm:ss')})

Dan_Askin
Level 4

Gotcha, Ryan. I am the moron who didn't check the box within the script -- but then I did check it and still no dice.

When I go to approve  the email or preview against subscribers, I get 'Cannot get email content- An error occurred when procesing (sic) the email Body!' Here's where I'm at from email script, to token name to html to rendering.

god-forsaken-email-scripting.png

god-forsaken-token-name.png

god-forsaken-email-code.png

date-format-rendering.png

Anonymous
Not applicable

Hi Dan,

Send yourself a sample using one of your preview leads. Script tokens won't render in preview mode (which is fairly worthless to begin with, IMO). Marketo has to actually run the script for it to insert anything and it will only do that when shooting an email out. Make sense?

Dan_Askin
Level 4

The above error is the message I get when I try to send the sample:

An error occurred when procesing the email Body!

Lexical error, Encountered: "\u201c" (8220), after : "" at *unset*[line 200, column 239] near

</tr>

<tr>

<td style="background-color:#ffffff;padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;border-collapse:collapse;">

  <tab

Anonymous
Not applicable

Hi Dan. Your parenthesis seem out of whack.

You have:

$date.format('MMM dd, yyyy hh:mm tt', ${convert.parseDate(${lead.client_saintsseatrelocation}), 'yyyy-MM-dd HH:mm:ss')})

Try:

$date.format('MMM dd, yyyy hh:mm tt', ${convert.parseDate($lead.client_saintsseatrelocation, 'yyyy-MM-dd HH:mm:ss')})

Also, I'm not sure waht "tt" is. AM/PM is actually lowercase "aa" if that's what you're looking for.

Jim_Clarke
Level 2

**Update**

Works like a charm I did one slight modification:

#set($str = $convert.parseDate(${OpportunityList.get(0).NextPhoneCallAppointment__c}, "yyyy-MM-dd HH:mm:ss"))

$date.format('medium',$str)

Ryan,

Thank you so much for the response, I really appreciate it.

Let me give it a try.

Jim