SOLVED

Pull Custom Object Record by Most recent Date in Field

Go to solution
Samantha_Cossum
Level 3

Hi all,

We store our customer's training history in a custom object. We use the velocity scripts to pull in the date of upcoming trainings and I am trying to take that script and adjust it to pull the last day they had a specific training but having issues getting it to pull the most recent date. 

I've read a bunch of articles on here and and around the web but I'm still struggling to get it to work/apply to our specific situation. Our code is a little messy because we pull off the training types (which have changed over the years) and for this specific use case, that the status of training is complete (due to a change in our sync the case changed so I'm accounting for that as well). The actually date I want to pull in is the most recent end date of the training. Below is my code.Everything I tried to add to make it work just broke the script so this is without any of tries at sorting. 

#set($startDate = $trainingRecord_cList)
#set($type = $trainingRecord_cList)
#foreach($event in $startDate)
#if(($event.trainingType == "New Patient Mastery On-Site Solution (New Member)" || $event.trainingType == "New Patient Mastery (New Member)" || $event.trainingType == "5 Star Telephone Training (New Member)" || $event.trainingType == "5 Star Telephone Training (New Member))" || $event.trainingType == "5 Star Telephone Training (Upgrade)" || $event.trainingType == "Advanced Telephone & Capacity Techniques" || $event.trainingType == "New Patient Mastery (Upgrade)" || $event.trainingType == "New Patient Mastery On-Site Solution (Upgrade)") && ($event.trainingStatus == "complete" || $event.trainingStatus == "Complete"))
#set($origDate = $convert.parseDate($event.trainingScheduledEndDate,"yyyy-MM-dd"))
#set($formattedDate = $date.format('full_date',$origDate))
#end
#end
$formattedDate

I'm still newer to Velocity and the scripts we use for our trainings were built beside an outside company before I came on so any suggestions would be very much appreciated! 

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator
#set( $interestingTypes = [
"New Patient Mastery On-Site Solution (New Member)",
"New Patient Mastery (New Member)",
"5 Star Telephone Training (New Member)",
"5 Star Telephone Training (New Member))",
"5 Star Telephone Training (Upgrade)",
"Advanced Telephone & Capacity Techniques",
"New Patient Mastery (Upgrade)",
"New Patient Mastery On-Site Solution (Upgrade)"
] )
#foreach( $event in $sorter.sort($trainingRecord_cList, "trainingScheduledEndDate:desc") )
#if( $interestingTypes.contains($event.trainingType) && $event.trainingStatus.equalsIgnoreCase("complete") )
#set( $latestEventDate = $event.trainingScheduledEndDate )
#break
#end
#end

This will give you $latestEventDate set to the latest date (still as the original date-like String, not true Date -- you can sort by date-like Strings without converting them, as long as the format is yyyy-MM-dd).

I'll leave it to you to format the date-like String to Date based on my blog post.

View solution in original post

12 REPLIES 12