A Few More Email Scripting Examples | Community
Skip to main content
March 22, 2016

A Few More Email Scripting Examples

I wanted to share a few more email scripting examples with you.

Example 1: This script lowercases a string (in this case First and Last Name) and then properly capitalizes it.

(I think the previous example I gave only capitalizes the first letter, so if you had mixed case or all capitalized letters, it is not as good as this one.)

#set ($fname = ${lead.FirstName.toLowerCase()})

#set ($lname = ${lead.LastName.toLowerCase()})

$display.capitalize($fname) $display.capitalize($lname)

Example 2: This script adds a date 14 days in the future onto a token being used in an email.

## Access Velocity's calendar object

#set($x = $date.calendar)

## Format date

#set($current_date = $date.full_date)

## Add 24 hours (hours=int code 10 - see list below)

$x.add(6,14)

## Show result

$current_date

Example 3: This script gets the current date from the calendar object, converts it to a date and then reformats it to the format highlighted in yellow.

#set ( $todayCalObj = $date.toCalendar($date.toDate("yyyy-MM-dd H:m:s",$date.get('yyyy-MM-dd H:m:s'))) )

#set ( $dateObj = $date.toDate("yyyy-MM-dd", $todayCalObj) )

#set($dateFormatted = $date.format("MM/dd/yyyy", $dateObj))

$dateFormatted

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

    12 replies

    SanfordWhiteman
    Level 10
    April 27, 2017

    @Benjamin Ortiz this may not be the best place to follow up (especially since there's no syntax highlighting here, as opposed to on Products threads).

    But while I wouldn't do it this way, the 10 and 336 represent, respectively, the Calendar class field HOUR (this is just a unique integer constant that has no intrinsic meaning) and 14 days in hours (14 days * 24 hours = 336 hours). Hence you're adding unit=HOUR, count=336.

    The more readable way, if you want for some reason to use hours, is:

    $Calendar.add($field.in($Calendar).HOUR, $math.mul(24,14))

    but it makes more sense to use days, so you don't have to do any internal arithmetic:

    $Calendar.add($field.in($Calendar).DATE, 14)

    Translate
    Benjamin_Ortiz1
    Level 4
    April 26, 2017

    Hi Jason, can you explain how this logic works for adding 14 days? $dateInXDays.add(10,336)

    I am trying to understand the math behind it. Thank you!

    Translate
    SanfordWhiteman
    Level 10
    February 21, 2017

    JD, if you search my Community posts on Velocity, you'll see several examples of this.

    Translate
    Community Manager
    February 21, 2017

    Hi Kristen Carmean​ -- I'm a bit late to this thread, but looking to expand #3, when you reference "calendar object" is this referencing 'todays date' (I believe, yes), and/or is there a way to pull the date listed in the calendar token?

    For example: We have an event that has a calendar token with a date of March 1. I want to be able to reformat that date (not today's date) in the method you've described; is that feasible?

    Translate
    June 3, 2016

    I believe you need to change the lead field into a calendar object. You see how in the example scripts, you do the $date.calendar? That's what this part is doing. So you are missing the step of converting your dateTime format into a calendar.

    Translate
    June 3, 2016

    I am trying use same logic for field of lead object rather than current date from calender, apparently it did not work any insight?

    my script is like this

    #set($dateInXDays = ${lead.Appointment})

    $dateInXDays.add(10,24)

    $date.format('full_date',$dateInXDays.time)

    Translate
    Jason_Hamilton1
    Level 6
    May 4, 2016

    Glad I could help

    Translate
    Valerie_Armstro
    Level 9
    May 3, 2016

    @Jason Hamilton​ - it works!  Thank you so much

    Translate
    Jason_Hamilton1
    Level 6
    May 3, 2016

    Hi Valerie,

    Try this

    #set($dateInXDays = $date.calendar)

    $dateInXDays.add(10,336)

    $date.format('full_date',$dateInXDays.time)

    Translate
    Valerie_Armstro
    Level 9
    May 3, 2016

    Hi Kristen Carmean - I'm looking to use the example you provided for the date 14 days in the future, but having some trouble getting it to work properly.  Currently, it keeps returning today's date instead of one 14 days out.  Is there something I am suppose to be changing in the code you provided?  Thanks!

    Translate