Velocity - multiple sessions - remove session dynamically from email body

Highlighted

Velocity - multiple sessions - remove session dynamically from email body

Hello!

I am looking for a neat solution in order to control dynamic display of event sessions, in email inviation.

Email body will have following table with sessions listed i.e.
Session 1 - APR 12th 2020
Session 2- APR 20th 2020
Session 3 - APR 20th 2020

Same invite will be sent before Session 2 and the list should only show:
Session 2- APR 20th 2020
Session 3 - APR 20th 2020

I was thinking of implementing below solution based on Sanford's blog post:

#set( $calSession1 = $convert.toCalendar(
  $convert.parseDate(
    "2020-04-04T00:00:00", //First invite
    $ISO8601DateTime, 
    $defaultLocale, 
    $defaultTimeZone 
  )
) )
#set( $calEndSession1 = $convert.toCalendar(
  $convert.parseDate(
    "2017-04-12T00:00:00", 
    $ISO8601DateTime, 
    $defaultLocale, 
    $defaultTimeZone 
  )
) )
#if( $calNow.compareTo($calSession1) >=0 && $calNow.before($calEndSession1) )
Session 1 - APR 12th 2020
Session 2- APR 20th 2020
Session 3 - APR 20th 2020

#else
REPEAT THE ABOVE WITH NEW DATES?
#end

should I repeat the above when Session 1 expires and when another invite is sent, starting from Session 2?

Appreaciate your help.

Thanks,
Kacper

2 REPLIES 2
Highlighted

Re: Velocity - multiple sessions - remove session dynamically from email body

Please include this global common variables at the top of the token, as I see you have used in your Email script 🙂

#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" )

Also I think that since you aim to display different content (3 dates, 2 dates and then finally 1 date based on the date of the email sendout and session date) each time you should consider repeating setting the start and end date for the session, also it would give you more visibility while script editing. Also would love to know what's Sanford's opinion over  this! 🙂

 

Best,
Darshil
Highlighted
Level 10 - Community Moderator

Re: Velocity - multiple sessions - remove session dynamically from email body

1. This will cause a fatal syntax error regardless of what else you do, as // is not a comment in Velocity:

 

    "2020-04-04T00:00:00", //First invite

 

2. I hope you're not actually hard-coding the dates inside the code. Very bad practice, so I'm going to assume you mean the dates would actually be in a separate list.

 

3. Like Darshil says, you're implying that you're using the rest of my code, but the common header variables should be included so your code is runnable.

 

Now, let us assume you have the dates in a list, and you want to show only future or ongoing dates.

 

Do that by creating another filtered list (that's the pattern to use with many, many Velocity situations). Then work with the filtered list.

 

## upcoming events, as date-like Strings; note this could (perhaps should) be in a separate token
#set( $events = [
  { 
    "startDate" : "2020-03-04T00:00:00",
    "endDate"   : "2020-03-20T00:00:00"
  },
  { 
    "startDate" : "2020-03-29T00:00:00", 
    "endDate"   : "2020-04-02T00:00:00"    
  },
  { 
    "startDate" : "2020-04-01T00:00:00",
    "endDate"   : "2020-04-07T00:00:00"    
  }
] )
## common Date math includes from https://blog.teknkl.com/velocity-days-and-weeks/
#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" )
## allocate new filtered list
#set( $filteredEvents = [] )
#foreach( $event in $events )
#set( $event.startCal = $convert.toCalendar(
  $convert.parseDate(
    $event.startDate,
    $ISO8601DateTime, 
    $defaultLocale, 
    $defaultTimeZone 
  )
) )
#set( $event.endCal = $convert.toCalendar(
  $convert.parseDate(
    $event.endDate,
    $ISO8601DateTime, 
    $defaultLocale, 
    $defaultTimeZone 
  )
) )
## replace this with your business logic
## example checks if event is currently happening OR completely in future
#if( $event.endCal.compareTo($calNow).equals(1) )
#set( $void = $filteredEvents.add($event) )
#end
#end
## now output the filtered list only
#foreach( $event in $filteredEvents )
$event.startDate $event.endDate
## whatever you want to display from each $event goes here
#end