Dates in Velocity Scripting - Anomalous Behavior | Community
Skip to main content
Vidhi_Kare
New Participant
June 6, 2022
Solved

Dates in Velocity Scripting - Anomalous Behavior

  • June 6, 2022
  • 2 replies
  • 2851 views

Hi Everyone,

 

Hope your week has started well.

I just came across a situation where  the  below script is running into error for a specific date but it is fine for other dates.

Example: When the leadcapturedate i.e. my object is having value '2021-09-03' or value = '2022-05-21' it is working fine, but when the object had value -'2022-05-29', it failed. 

2. Also, may I know if this is the right way to display date in single digit in my code below in line #3 .for example date is 2021-09-03 then it is displaying 3 September 2021, which is what I want. But just wanted to confirm.

**** Code****

#set($myCal = $convert.toCalendar(${convert.parseDate(${lead.as_leadcapturedate},'yyyy-MM-dd')})) #set($calConst = $field.in($myCal)) #set($FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ ='d MMMM yyyy') $myCal.add($calConst.DATE,30) ${date.format($FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ,$myCal)}

Any inputs in this regard will be appreciated.

 

Kind Regards,

Vidhi

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Darshil_Shah1

Can you try using below velocity script instead? Change the time zone as per your preference, refer IANA time zone directory for the time zone abbreviations.

#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.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) #set($dateIn = $convert.parseDate( $lead.as_leadcapturedate, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone )) #set($dateOut = $date.format( "d MMMM yyyy", $dateIn, $defaultLocale, $defaultTimeZone )) ${dateOut}

2 replies

SanfordWhiteman
New Participant
June 6, 2022

I just came across a situation where  the  below script is running into error for a specific date but it is fine for other dates.

Example: When the leadcapturedate i.e. my object is having value '2021-09-03' or value = '2022-05-21' it is working fine, but when the object had value -'2022-05-29', it failed. 


What do you mean, exactly, by “it is working” vs. “it failed”? Always need details for this kind of question.

Vidhi_Kare
New Participant
June 9, 2022

Hi Everyone,

Apologies for the delayed response.

@sanfordwhiteman : Sorry for not providing detailed explanation regarding the behaviour that I noticed in the token code working.

For Example: When the token received a date '2021-09-03' or value = '2022-05-21' then it is able to render the code in email where it is used and desired output is displayed.

However, not too sure what happens to the token as I did not debug the step by step code but , it flashes error on email while rendering the same token for the date ''2022-05-29" . Perhaps, I would need to add code to handle the null value in the script.

Also this date value is coming in Marketo from lead object field from our CRM database.

It sounds little odd but I will spend some more time in the step by step debugging of existing code.

The token code is fetching value for the field 'leadcapturedate' and after parsing , it is adding 30 days more to display the desired date in our email.

#set($myCal = $convert.toCalendar(${convert.parseDate(${lead.as_leadcapturedate},'yyyy-MM-dd')}))
#set($calConst = $field.in($myCal))
#set($FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ ='d MMMM yyyy')
$myCal.add($calConst.DATE,30)
${date.format($FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ,$myCal)}

 

 

Kind Regards,

Vidhi

SanfordWhiteman
New Participant
June 9, 2022

However, not too sure what happens to the token as I did not debug the step by step code but , it flashes error on email while rendering the same token for the date ''2022-05-29" . Perhaps, I would need to add code to handle the null value in the script.


You should always be aware of the possibility of null values, but there’s no reason to expect a null here if the date is non-empty and formatted correctly.

 

Is this a true Date field in Marketo, or a String field that happens to be filled with a date-like ISO 8601 value?

Darshil_Shah1
Darshil_Shah1Accepted solution
Community Manager
June 6, 2022

Can you try using below velocity script instead? Change the time zone as per your preference, refer IANA time zone directory for the time zone abbreviations.

#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.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) #set($dateIn = $convert.parseDate( $lead.as_leadcapturedate, $ISO8601DateOnly, $defaultLocale, $defaultTimeZone )) #set($dateOut = $date.format( "d MMMM yyyy", $dateIn, $defaultLocale, $defaultTimeZone )) ${dateOut}