Velocity email scripting - sorting a list

Phillip_Wild
Level 10

Curly one for you guys here...it's been driving me crazy.

We're digging into email scripting with our Salesforce custom objects. Specifically, I'm trying to extract a list of dates from a custom object, sort them descending, and return the number of days that the highest date is from today. Here's an example...

A customer buys a service that departs on the 1st March 2014, 4th September 2014 and the 11th July 2015. The answer I am looking for is 11th July 2015 - today's date = 5.

Here's the code so far:

#set($ServicesArray = [])

## Set a blank array called ServicesArray - all good so far!

#foreach ($srv in ${TravellerServices__cList})

  #if (${srv.ServiceTypeTS__c} == "Tour")

#set($Elements = $ServicesArray.add(${srv.Service_Start_Date__c}))

      #end

## For each service in TravellerServices__cList (our Salesforce object), if it's a Tour type, then add it to my blank ServicesArray. I know this works correctly because when I print the list after this point, the output is [date1, date2, date3...date n].

#foreach($srv in $sorter.sort($ServiceArray, ["$Service_Start_Date__c:asc"]))

    $srv.Service_Start_Date__c

#end

## This is my attempt at a sort based on some googling Apache Velocity - Velocity User Guide  and SortTool (VelocityTools 2.0-beta4 Documentation) . This is where it stops working. I don't really know the syntax I should be using, and any attempts to sort either fail in Marketo (I get an error upon sending sample email) or the sample email sent is never delivered (which I can only assume is an error too).

Additionally, I haven't even been able to get something as simple as "return me n element from this list" to work using a function such as $ServiceArray.get(0) . This purely prints that text in the email. I've also tried setting it to a variable first  - #set ($DateRequired = $ServiceArray.get(0)) with no success either.

Any ideas? Gold star for anyone who can help!

Tags (2)
12 REPLIES 12