SOLVED

2 months after lastactivity date in script token

Go to solution
nitenichiryu
Level 2

2 months after lastactivity date in script token

Is it possible to create a date script token in an e-mail which shows 2 months after from the last activity date?
I understand I can get the last activity date as OpportunityList.get(0).LastActivityDate.

1 ACCEPTED SOLUTION

Accepted Solutions
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: 2 months after lastactivity date in script token

Could you try the below script?

 

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Asia/Tokyo") )
#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( $sortedList = $sorter.sort($OpportunityList,"LastActivityDate"))
##sort the list in ascending (asc)/descending (desc) order as required

#set( $dateObj = $convert.toCalendar(
  $convert.parseDate(
    $sortedList.get(0).LastActivityDate,
    $ISO8601DateOnly, 
    $defaultLocale,
    $defaultTimeZone 
  )
) )

$dateObj.add($calConst.MONTH, 2)

${date.format(
  $ISO8601DateOnly,
  $dateObj,
  $defaultLocale,
  $defaultTimeZone
)}

 

 

View solution in original post

7 REPLIES 7
SanfordWhiteman
Level 10 - Community Moderator

Re: 2 months after lastactivity date in script token

First, no: you can’t trust OpportunityList.get(0).LastActivityDate to be the last activity date across all Opportunities. You must get a sorted list with $sorter.sort($OpportunityList,"LastActivityDate") to do that. Never assume the list starts out in a certain order.

 

I can’t recommend too strongly that you read the seminal post on date math in Velocity:

Velocitips: Switch email content based on day/time

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: 2 months after lastactivity date in script token

Agreed with Sandy! His article on Switch email content based on day/time is absolutely great with all the great examples and relevant use cases! There's one use-case with the code snippet for adding days to the date as well that you can refer to. 🙂

 

nitenichiryu
Level 2

Re: 2 months after lastactivity date in script token

Thank you Sandy and Darshil,

 

#set($calNow = $sorter.sort($OpportunityList,"LastActivityDate"))

But how should I add 2 months to ${calNow[0]}, because based on the Velocitips: Switch email content based on day/time which you shared, I tried by using "Promo expires 7 days from today" as reference, but it does not work. 


SanfordWhiteman
Level 10 - Community Moderator

Re: 2 months after lastactivity date in script token

Don’t understand why you’re using the variable $calNow here. That variable is already used for a Calendar object (effectively “today’s calendar”) in the standard Velocity day/time boilerplate. If you reassign $calNow you break everything else.

 

The name of the sorted list should be like $sortedOpportunityList.

 

It’s best if you show what you tried with the date math, we can’t tell you what’s wrong without seeing code.

nitenichiryu
Level 2

Re: 2 months after lastactivity date in script token

HI Sandy 
Thank you for your comment.

Could you check the below?

-- Purpose

To send the marketing campaign promotion to the customers who terminates the contract 

e.g. 
The customer A : terminate the contract Jan. 1st
Send the mail B on Feb 1st

In the mail B, I want to include the following statement

The special promotion will ends on Mar 1st
(→ This date should be defined as 2 months after the termination date.)

-- Script Token

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Asia/Tokyo") )
#set( $defaultLocale = $date.getLocale() )
#set( $sortedList = $sorter.sort($OpportunityList,"LastActivityDate"))
→ {LastActivityDate=2023-01-21, MarketoCreatedAt=2022-02-08 23:14:56, LastStageChangeDate=2023-01-11 00:09:25}

I understand $sortedList object is different from $date.getCalendar() and should handle in a different way. 
But I do not know how I should handle to add 2 months to LastActivityDate.

Please advise.

 

 

 

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: 2 months after lastactivity date in script token

Could you try the below script?

 

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Asia/Tokyo") )
#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( $sortedList = $sorter.sort($OpportunityList,"LastActivityDate"))
##sort the list in ascending (asc)/descending (desc) order as required

#set( $dateObj = $convert.toCalendar(
  $convert.parseDate(
    $sortedList.get(0).LastActivityDate,
    $ISO8601DateOnly, 
    $defaultLocale,
    $defaultTimeZone 
  )
) )

$dateObj.add($calConst.MONTH, 2)

${date.format(
  $ISO8601DateOnly,
  $dateObj,
  $defaultLocale,
  $defaultTimeZone
)}

 

 

nitenichiryu
Level 2

Re: 2 months after lastactivity date in script token

Hi Sandy,

It works as expected.

 

Thank you for your support.