2 months after lastactivity date in script token | Community
Skip to main content
New Participant
March 15, 2023
Solved

2 months after lastactivity date in script token

  • March 15, 2023
  • 2 replies
  • 3174 views

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.

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

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.

 

 

 


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 )}

 

 

2 replies

Darshil_Shah1
Community Manager
March 15, 2023

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. 🙂

 

New Participant
March 16, 2023

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
New Participant
March 16, 2023

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.

SanfordWhiteman
New Participant
March 15, 2023

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