New to Velocity - How do I get the most recent object record? | Community
Skip to main content
Melissa_Neikirk
Level 2
June 25, 2018
Question

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

  • June 25, 2018
  • 1 reply
  • 5459 views

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!

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

1 reply

SanfordWhiteman
Level 10
June 26, 2018

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
June 26, 2018

Thanks, Sanford! I appreciate your quick response.

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

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
June 26, 2018

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