Hello there!
I'm new to Velocity and need some help. I'm trying to list all the names of approved applications within the last 12 hours in an email. We have a custom object with approved timestamp and the name of the application.
#set ($sortedUpdated = $sorter.sort($appRecord_cList,"ApprovalTimestamp:desc"))
#foreach($Record in $sortedUpdated)
#if (!(!$!Record.ApprovalTimestamp || $Record.ApprovalTimestamp.isEmpty()))
#set ($name = "$Record.Name")
#end
#end
$name
I have not gotten far with this, just trying to list the names first before adding the time component to it. Any help is appreciated, thank you!!
Solved! Go to Solution.
Try the below script! Make sure to update the correct date time format variable in the set $convtdApprovalTimestamp variable piece as per the date time format of the data you've in the field "ApprovalTimestamp". Currently, it's set to $ISO8601DateTime.
Also, instead of looping through entire list and checking whether the difference is less than 12 hrs before printing the Record.Name, you can also print data until the loop encounters the first record where the difference becomes > 12 hours; #break could help breaking out of the loop as soon as the difference becomes > 12 hours. This should work as we're sorting the list in descending order as per ApprovalTimestamp field's value".
You should also check out Sandy's Velocitips: Switch email content based on day/time blog post to understand how to deal with the date/time-responsive content in general.
#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.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
#set ($sortedUpdated = $sorter.sort($appRecord_cList,"ApprovalTimestamp:desc"))
#foreach($Record in $sortedUpdated)
#if (!($Record.ApprovalTimestamp.isEmpty()))
#set( $convtdApprovalTimestamp=$convert.toCalendar(
$convert.parseDate(
$Record.ApprovalTimestamp,
$ISO8601DateTime,
$defaultLocale,
$defaultTimeZone
)
))
#if($date.difference($convtdApprovalTimestamp,$calNow).getHours() > 12)
#break
#end
${Record.Name}
#end
#end
in the past 12 hours? I saw something in this post but unsure how to apply it to my Velocity script. Would it need to be something like this?
<li>${display.list($allName,", ")}</li> or <li>${appRecord_cList.get(0).Name}</li>
Certainly not the latter. That’s seeking the first (0-th) record in a list, which is a sign you’re doing something wrong in almost any context. (The only exception is when you’ve deliberately sorted a list first.)
The former will display every item in the list $allName. There’s no list by that name in your code, nor in Darshil’s. So it has no meaning.
If you want to create an <li> for every item in a list you don’t need $display.list. (You can use it, but the syntax gets unreadably complex: $display.list is supposed to be for simple comma-/semicolon-delimited output, not more complex HTML).
To output <li> elements:
#if( !$aList.isEmpty() )
<ul>
#foreach( $item in $aList )
<li>${item.property}</li>
#end
</ul>
#end
I don't understand your counter logic. Where and how are you incrementing your counter? How are you updating the $velocityCount variable?
I'd just add names to a velocity array, and then reference its size to format it differently in case there are multiple records in it. Could you try the below script and see if it prints the desired output?
#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.SSS'Z'" )
#set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" )
#set ($sortedUpdated = $sorter.sort($appRecord_cList,"ApprovalTimestamp:desc"))
#set($names = [])
#foreach($Record in $sortedUpdated)
#if (!($Record.ApprovalTimestamp.isEmpty()))
#set( $convtdApprovalTimestamp=$convert.toCalendar(
$convert.parseDate(
$Record.ApprovalTimestamp,
$ISO8601DateTime,
$defaultLocale,
$defaultTimeZone
)
))
#if($date.difference($convtdApprovalTimestamp,$calNow).getHours() > 12)
#break
#end
#set($result = $names.add($Record.Name))
#end
#end
#if($names.size().equals(1))
<p style="margin: 0 0; mso-line-height-alt: 29px;">A single ${names.get(0)}</p>
#elseif ($names.size() > 1)
<p style="margin: 0 0; mso-line-height-alt: 29px;">Here are the list of names:<br><br></p>
#foreach( $loopVar in $names )
${loopVar}<br/>
#end
#end