New to Velocity - How do I get the most recent object record?

Melissa_Neikirk
Level 2

New to Velocity - How do I get the most recent object record?

This has been asked a few times, and I have looked through and tried most of those solutions. However, in my case it still seems not to be working, and I am not sure why.

Here is an example of the cleanest script I have tried, an adaption of script given to another user for the same issue.

#if( $AdPotentialCancellation_cList && !$AdPotentialCancellation_cList.isEmpty() )

#set( $firstItem = $AdPotentialCancellation_cList[0] )

#set( $lastItem = $AdPotentialCancellation_cList[$math.sub($AdPotentialCancellation_cList.size(),1)] )

#end

$lastItem.Name

Some things to note: AdPotentialCancellation is the name of the SFDC Custom Object being referenced. 'Name' is the field I am trying to get to populate in the email with the above script. I have other fields that I also need to populate in the email.

These are the other fields I would like to populate in separate email scripts in the email: 'Declined_Amount__c' & 'Decline_Date__c'. I have done some work on formatting the amount field to be currency and the date to be a date -- it works only if I used the standard code that grabs the oldest field.

Decline Amount Code:

#set($amount = $number.format("$0", ${AdPotentialCancellation__cList.get(0).Declined_Amount__c}))

${amount}

Decline Date Code:

#set( $dateOptions = { 

  "formats" : { 

    "userin" : "yyyy-MM-dd", 

    "userout" : "MMMM dd, yyyy" 

  }, 

  "timezones" : { 

    "userin" : "America/Los_Angeles", 

    "userout" : "America/Los_Angeles" 

  }, 

  "locale" : $date.getLocale() 

} ) 

#set( $declineDatelike = ${AdPotentialCancellation__cList.get(0).Decline_Date__c} ) 

#set( $declineDate = $convert.parseDate( 

  $declineDatelike, 

  $dateOptions.formats.userin, 

  $dateOptions.locale, 

  $date.getTimeZone().getTimeZone($dateOptions.timezones.userin) 

) ) 

#set( $declineDate_formatted = $date.format( 

  $dateOptions.formats.userout, 

  $declineDate, 

  $dateOptions.locale, 

  $date.getTimeZone().getTimeZone($dateOptions.timezones.userout) 

) ) 

${declineDate_formatted}

An issue I keep seeingis that the email renders the $lastItem.Name, ${amount} or  ${declineDate_formatted} instead of any data. I used the below code here to confirm that the data exists for the test lead on the object, and made sure to check all the fields I need in the right column to see the full list. I can see data is there for all fields.

#if( !$context.contains("AdPotentialCancellation__cList") ) 

  AdPotentialCancellation list does not exist in context. 

#else 

  AdPotentialCancellation list exists. 

  #set( $listSize = $AdPotentialCancellation__cList.size() ) 

  #if( $listSize == 0 ) 

    List is empty. 

  #else 

    List size: $listSize. 

    List values: ${display.list($AdPotentialCancellation__cList,"<br>")} 

  #end 

#end 

Can anyone help me understand where I might be going wrong, or other ways I can troubleshoot the code? Thank you!

10 REPLIES 10
SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity - How do I get the most recent object record?

What you've left out is any example of the data actually stored in these objects, and the errors/output you're seeing.

What's the output (in the text part of an email, as that's easier to troubleshoot) of:

$display.list($AdPotentialCancellation_cList,"\u000a")

Anyway, there's nothing inherently wrong with your first bit of code, but if it doesn't match the shape of the ArrayList of objects then it's going to throw an error:

Note you can order the list by any field (as long as it's checked off in Script Editor). For example, to order by your Decline_Date__c in ascending order:

#set( $objectsByDeclineDateAsc = $sorter.sort($AdPotentialCancellation_cList,"Decline_Date__c") )

#set( $firstItem = $objectsByDeclineDateAsc )

#set( $lastItem = $objectsByDeclineDateAsc )

Melissa_Neikirk
Level 2

Re: New to Velocity - How do I get the most recent object record?

Thanks, Sanford! I appreciate your quick response.

When I input the code above what I see in the email text is this:

pastedImage_0.png

When I copy the code above exactly for ordering by Decline_Date_c the email text display is empty/blank.

To your point about not showing what displays in the email for the code in my original post, here is what happens using the same lead for each code instance.

  • When I do the code to get the most recent Name (which is our version of a case number) this is what displays in the email.
    • $lastItem.Name.
  • When I do the troubleshooting code from my original post this is what displays in the email.
    • AdPotentialCancellation list exists.
    • List size: 2.
    • List values: {Declined_Amount__c=1493.99, Decline_Date__c=2017-07-26, Name=A-469957, Status__c=Saved} {Declined_Amount__c=150, Decline_Date__c=2018-06-26, Name=A-561375, Status__c=Pending}
  • When I do the date code from my original post this is what displays in the email.
    • July 26, 2017
SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity - How do I get the most recent object record?

Something's not quite right in your testing setup.  If $AdPotentialCancellation_cList is in the Velocity context (checked off in the tree in Script Editor) and is not empty, then this line cannot fail:

$display.list($AdPotentialCancellation_cList,"\u000a")

Are you testing using Preview by List with a person known to have AdPotentionalCancellation records?

List values: {Declined_Amount__c=1493.99, Decline_Date__c=2017-07-26, Name=A-469957, Status__c=Saved} {Declined_Amount__c=150, Decline_Date__c=2018-06-26, Name=A-561375, Status__c=Pending}

If I create a list exactly like this, I don't have a problem.

Setup:

#set( $AdPotentialCancellation_cList = [

{

  "Declined_Amount__c" : 1493.99,

  "Decline_Date__c" : "2017-07-26",

  "Name" : "A-469957",

  "Status__c" : "Saved"

},

{

  "Declined_Amount__c" : 150,

  "Decline_Date__c" : "2018-06-26",

  "Name" : "A-561375",

  "Status__c" : "Pending"

}

] )

#if( $AdPotentialCancellation_cList && !$AdPotentialCancellation_cList.isEmpty() )  

#set( $firstItem = $AdPotentialCancellation_cList[0] )  

#set( $lastItem = $AdPotentialCancellation_cList[$math.sub($AdPotentialCancellation_cList.size(),1)] )  

All items:

$display.list($AdPotentialCancellation_cList,"\u000a")

First item: $firstItem.Name

Last item: $lastItem.Name

#end

Output:

All items:

{Declined_Amount__c=1493.99, Decline_Date__c=2017-07-26, Name=A-469957, Status__c=Saved}

{Declined_Amount__c=150, Decline_Date__c=2018-06-26, Name=A-561375, Status__c=Pending}

First item: A-469957

Last item: A-561375

Melissa_Neikirk
Level 2

Re: New to Velocity - How do I get the most recent object record?

I tested it as a 'send alert' send to my email for the specific lead record that has data on that object as well as a test sample send spoofing that lead in addition to just previewing it as a person in the preview panel. The results are the same. The list won't populate. it just displays the code.

Here is what that token looks like when I have it in script and what is checked on the right.

pastedImage_0.png

However, when I do this code with the same boxes checked in the script token - it populates in the email in all ways to listed above for testing.

Setup:

#if( !$context.contains("AdPotentialCancellation__cList") )  

  AdPotentialCancellation list does not exist in context.  

#else  

  AdPotentialCancellation list exists.  

  #set( $listSize = $AdPotentialCancellation__cList.size() )  

  #if( $listSize == 0 )  

    List is empty.  

  #else  

    List size: $listSize.  

    List values: ${display.list($AdPotentialCancellation__cList,"<br>")}  

  #end  

#end

Output:

AdPotentialCancellation list exists.

List size: 2.

List values: {Declined_Amount__c=1493.99, Decline_Date__c=2017-07-26, Name=A-469957, Status__c=Saved} {Declined_Amount__c=150, Decline_Date__c=2018-06-26, Name=A-561375, Status__c=Pending}

This data is coming through a salesforce custom object, does that impact any of the ways we could test this?

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity - How do I get the most recent object record?

I tested it as a 'send alert' send to my email for the specific lead record that has data on that object as well as a test sample send spoofing that lead in addition to just previewing it as a person in the preview panel.

The only ways to reliably test Velocity are:

  • Preview by List (with a Static List of people with values populated)
  • Send actual email (not sample)

In production, Send Alert will work, but I never test that way.

Sample must never be used unless all of your content is generated within Velocity, i.e. nothing from the Lead or from COs at all.

This data is coming through a salesforce custom object, does that impact any of the ways we could test this?

No, it doesn't, but your results still don't make sense.  You seem to be saying that:

$display.list($AdPotentialCancellation_cList,"\u000a")

fails while

$display.list($AdPotentialCancellation_cList,"<br>")

works.

But I think you're testing across different actual tokens so you're lacking a direct comparison.

What happens if you create a new token and use my exact code above for generating the object list in VTL? Don't check off the in object in the tree at first. Test with the mock list. Then remove the mock list and check off the object + fields in the tree and test again.

Melissa_Neikirk
Level 2

Re: New to Velocity - How do I get the most recent object record?

Thank you for clarifying what best testing practices are and when to use them - that is really helpful!

I do have one token that is a lead token in the email in addition to the three different velocity tokens.

When I test using your code exactly and nothing checked off in the object tree on the right - it populates with this in the email:

All items: {Declined_Amount__c=1493.99, Decline_Date__c=2017-07-26, Name=A-469957, Status__c=Saved} {Declined_Amount__c=150, Decline_Date__c=2018-06-26, Name=A-561375, Status__c=Pending} First item: A-469957 Last item: A-561375

When I only use this portion of the code in addition to checking off the fields in the right object tree:

#if( $AdPotentialCancellation_cList && !$AdPotentialCancellation_cList.isEmpty() )    

#set( $firstItem = $AdPotentialCancellation_cList[0] )    

#set( $lastItem = $AdPotentialCancellation_cList[$math.sub($AdPotentialCancellation_cList.size(),1)] )    

All items:  

$display.list($AdPotentialCancellation_cList,"\u000a") 

First item: $firstItem.Name 

Last item: $lastItem.Name 

#end

It does not show anything in the email output.

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity - How do I get the most recent object record?

Which would be consistent with the _cList being empty for the lead you're testing with.

Melissa_Neikirk
Level 2

Re: New to Velocity - How do I get the most recent object record?

But that is just it, I don't think it is empty. When I check that lead in the database, there is data for this object in their lead details in the activity log. As well, the other code I was using to determine what data was on the object works.

Setup:

#if( !$context.contains("AdPotentialCancellation__cList") )   

  AdPotentialCancellation list does not exist in context.   

#else   

  AdPotentialCancellation list exists.   

  #set( $listSize = $AdPotentialCancellation__cList.size() )   

  #if( $listSize == 0 )   

    List is empty.   

  #else   

    List size: $listSize.   

    List values: ${display.list($AdPotentialCancellation__cList,"<br>")}   

  #end   

#end

Output:

AdPotentialCancellation list exists. List size: 2. List values: {Declined_Amount__c=1493.99, Decline_Date__c=2017-07-26, Name=A-469957, Status__c=Saved}

{Declined_Amount__c=150, Decline_Date__c=2018-06-26, Name=A-561375, Status__c=Pending}

SanfordWhiteman
Level 10 - Community Moderator

Re: New to Velocity - How do I get the most recent object record?

There's no way for the list to at once [1] exist in context, [2] be non-empty, and [3] not pass the check for is-in-context and is-not-empty