SOLVED

Velocity - list values from a field in custom object

Go to solution
noorgramke
Level 1

Velocity - list values from a field in custom object

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!!

3 ACCEPTED SOLUTIONS

Accepted Solutions
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

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

 

 

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - list values from a field in custom object


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

View solution in original post

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

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

View solution in original post

13 REPLIES 13
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

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

 

 

noorgramke
Level 1

Re: Velocity - list values from a field in custom object

Thank you so much Darshil for your help! 🙌 I'm still stuck on how to display a bulleted list of 

 

${Record.Name}

 

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>
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

Well, assuming the "Name" is a field on the custom object, the script that I shared above should be able to print Application Names of all records associated with the person having ApprovalTimestamp in the past 12 hours. 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - list values from a field in custom object


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
Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

Agreed. I think OP just needs the regular display statement (something like: ${Record.Name}) when the condition is met (i.e., when ApprovalTimestamp is <= 12 hours of the current time).

 

 

noorgramke
Level 1

Re: Velocity - list values from a field in custom object

Ahh it works now!! I had some html in the token that caused some issues but I've removed them. Now all the values are populating in the body copy!

Really appreciate your help and patience, Darshil and Sanford. You are both amazing, thank you so much!

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

I'm glad to hear that the issue has been resolved and that the values are now populating in the email body! It's always important to make sure that the velocity tokens are properly formatted without any unexpected HTML tags or errors.

 

noorgramke
Level 1

Re: Velocity - list values from a field in custom object

Hi Darshil & Sanford,


I have another question that you might be able to help with. I'm trying to show the list if the count is more than 1. If there is only one record, I want it to be formatted differently. When I added the code below, the list is not showing all the names, it only shows one. I've tried manipulating it in different ways but still couldn't get it to work. Hoping you can see something that I've missed? Thank you in advance for your time and help!

 

#if($date.difference($convtdApprovalTimestamp,$calNow).getHours() > 12)
	#break
#end
	#set ($name = "${Record.Name}")
	#set ($thelist = "<li>${Record.Name}</li>")
	#set ($count = ${velocityCount})
#end
#end

#if($count.equals("1"))
<p style="margin: 0 0; mso-line-height-alt: 29px;">A single $name</p>
#else
<p style="margin: 0 0; mso-line-height-alt: 29px;">Here are the list of names:<br><br></p>
<ul>$thelist</ul>
#end

 

Darshil_Shah1
Level 10 - Community Advisor + Adobe Champion

Re: Velocity - list values from a field in custom object

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