SOLVED

Date Comparisons in Velocity Scripting

Go to solution
s_skala
Level 2

Date Comparisons in Velocity Scripting

I am currently working through a problem where I need to reference the service contract object in SFDC and pull the end date of that contract into an email in Marketo. Referencing the object is easy enough, however making sure the field is consistently populated with the correct date has proved a challenge. 

 

A customer can have more than one service contract with varying end dates and while the smart campaign might be triggered off the correct date, that date is not reliably populated using the email script token.

 

Is there any way that I can incorporate some date reference logic into the scripting token to pull only the date of the service contract expiring 15 days after the current date? Something along the lines of: 

 

#if ( Service_contract.enddate == getdate() + 15 )
Service_contract.enddate
#else 
** Some Text **
#end

 

I've looked through the Velocity documentation and haven't been able to find anything explicitly stating how to compare dates using a dynamic date reference.  Obviously the above is not syntactically correct, does anyone have any ideas how to make this work in practice? 

 

Thanks! 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Date Comparisons in Velocity Scripting

You definitely need to read my seminal post on datetime math in Velocity, like Phil says.

 

As you loop over the list, create a Calendar object for each of your end date properties. Then check if

 

$date.difference($calNow,$calEndDate).getDays().equals(15)

 

 

View solution in original post

4 REPLIES 4
Phillip_Wild
Level 10

Re: Date Comparisons in Velocity Scripting

Hi @s_skala ,

 

Good news and bad news: yes, it's certainly possible. But dates open up a world of complexity in Velocity 🙂 

 

@SanfordWhiteman has created some excellent resources on his blog: https://blog.teknkl.com/velocity-days-and-weeks/

 

My advice would be: start simple. See if you can get today's date to show up, using the guide there. Then try to add 15 days. Finally, compare the two Calendar objects.

 

It's not easy, but rewarding! Post back with any progress or if you have any questions.

s_skala
Level 2

Re: Date Comparisons in Velocity Scripting

Thanks for the resources @Phillip_Wild ! 

 

I feel like I might be overcomplicating the issue for myself though. The criteria for my automated email send enrolls all contacts with services contracts that end 15 days from today:

 

"Member of Smart List - Service Contract Ends in 15 days" and NOT IN "Member of Smart List - Service Contract Ends in 14 Days."

 

Given that, I'm reasonably sure that we should only be mailing contacts that have a service contract expiring 15 days from today so all I would have to do is create an email script token that populates a date 15 days from the current date. I'm not sure if the added complexity of looping through all the possible end dates is necessary given the end date I want to populate will always be 15 days from today. With that I could specify in my token: 

 

 

 

#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( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" )
#set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )

$calNow.add($calConst.DATE,15)
#set( $FRIENDLY_DATE = "MMM dd, yyyy" )
${date.format(
  $FRIENDLY_DATE,
  $calNow,
  $defaultLocale,
  $defaultTimeZone
)}

 

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Date Comparisons in Velocity Scripting

That's true, if you're already sure they have a Service Contract matching 15 days from now, you can add() to a Calendar object and output that.

 

Then again you could also set a Date field to

 

  {{system.date}} + 15 days

 

using Change Data Value, if you don't need to do any friendly formatting of the date.

SanfordWhiteman
Level 10 - Community Moderator

Re: Date Comparisons in Velocity Scripting

You definitely need to read my seminal post on datetime math in Velocity, like Phil says.

 

As you loop over the list, create a Calendar object for each of your end date properties. Then check if

 

$date.difference($calNow,$calEndDate).getDays().equals(15)