Use Email Script Token to calculate number of years based on a date field

A document by KCS Integration on behalf of e7a0d1ecd0b6a45a9b2b95cce957c9fa48144e95 on Mar 29, 2018Last modified by Roxann McGlumphy on Jun 3, 2019
Version 4Show Document
  • View in full screen mode

Issue Description

How to use a script token to calculate and populate the number of years since a given date


Issue Resolution
This can be achieved by using a velocity (email script) 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( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" ) 

#set( $ISO8601DateOnly = "yyyy-MM-dd" ) 

#set( $calJoinDate = $convert.toCalendar( 

$convert.parseDate( 

    $lead.JoinDate,  

    $ISO8601DateOnly,  

    $defaultLocale,  

    $defaultTimeZone  

  ) 

) ) 

#set( $differenceInYears = $date.difference($calJoinDate,$calNow ).getYears() ) 

#set( $friendlyLabel = $display.plural($convert.toInteger($differenceInYears),"year") ) 

You joined us ${differenceInYears} ${friendlyLabel} ago! 


Where $lead.JoinDate is the joining date. More at http://blog.teknkl.com/velocity-days-and-weeks/

Attachments

    Outcomes