AnsweredAssumed Answered

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

Question asked by Melissa Neikirk on Jun 25, 2018
Latest reply on Jun 26, 2018 by Melissa Neikirk

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!

Outcomes