Email Script Question

Jason_Scott
Level 4

Email Script Question

I'm looking to use an email script to set a url based on a SFDC custom object which would contain the eventdate and eventname. Below is what I'm looking to do.

For the event custom object

if the eventDate is today and eventName is conference then insert url A

if the eventDate is today and eventName is meeting then insert url B

Else insert url C

Seems so simple and I'm sure this is something that can be done but I'm not sure where to start.  Have you implemented something similar?

Tags (1)
6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Question

You're talking about a list of CO records (it's always a list, even if it's a list of 0 or 1). 

Are you outputting every record? (Yes, this matters a lot due to the vagaries of Marketo link tracking.)

Jason_Scott
Level 4

Re: Email Script Question

No, not looking to output every record.  There should only be one record per day max and I'm only looking to output if the eventdate is today.

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Question

"should" isn't good enough for a spec though.

Let's say you will

  • sort the list by by eventDate, descending
  • output the first record whose eventDate matches today, if such a record is present

What timezone are the eventDate values based on (yes, this too matters)?

And have you read such things as https://blog.teknkl.com/velocity-days-and-weeks/ ?

Jason_Scott
Level 4

Re: Email Script Question

Thanks for sharing the link, I'll take a look.  There's only on record per day max.  Timezone is eastern.

SanfordWhiteman
Level 10 - Community Moderator

Re: Email Script Question

## system default is Chicago, set your object time here
#set( $timeZones = {
"system" : $date.getTimeZone().getTimeZone("America/Chicago"),
"eventObject" : $date.getTimeZone().getTimeZone("America/New_York")
} )
## map names to URLs
#set( $eventURLs = {
"meeting" : "www.example.com/meeting/123",
"conference" : "www.example.com/conf/456"
} )
## fallback
#set( $defaultURL = "www.example.com/generic/789" )
## standard tz boilerplate
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $calConst = $field.in($calNow) )
#set( $void = $calNow.setTimeZone($timeZones.system) )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
#set( $partsNow = [$calNow.get($calConst.YEAR),$calNow.get($calConst.MONTH),$calNow.get($calConst.DATE)] )
## loop over events, break on match
#foreach( $event in $event_cList )
#set( $calEvent = $convert.toCalendar($convert.parseDate($event.eventDate, $ISO8601DateOnly, $defaultLocale, $timeZones.eventObject) ) )
#set( $partsEvent = [$calEvent.get($calConst.YEAR),$calEvent.get($calConst.MONTH),$calEvent.get($calConst.DATE)] )
## use List.equals(List), rather than stringifying
#if( $partsEvent.equals($partsNow) )
#set( $focusedEvent = $event )
#break
#end
#end
#if( $focusedEvent )
#set( $focusedEvent.effectiveURL = $display.alt($eventURLs[$focusedEvent.eventName], $defaultURL) )
<a href="https://${focusedEvent.effectiveURL}">click for your ${focusedEvent.eventName}</a>
#end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Where $event_cList is your CO and is of course checked off in the tree in Script Editor.

Note Velocity does not have a list filtering method, so the approach is loop-match-break. Also, though you may be tempted to use string comparisons for dates, don't. Properly parse them to Calendar objects and compare the constituent fields.

Jason_Scott
Level 4

Re: Email Script Question

Wow thank you! I'll give this a try.