Let me start off by saying I have as much experience with velocity scripting as I do with underwater basket weaving.
With that said, my goal is to be able to pull field values from the latest created salesforce custom object for a lead and reference them in an email.
For example, we have a custom object called "Transaction Queue" which is created every time a payment is scheduled. I want to reference values such as "Amount" and " Schedule Date" in an email that's triggered to send when this record is created.
I've tried various scripts based on what I've found in the community but nothing that is consistently pulling the latest record.
Any help is greatly appreciated 🙂
Solved! Go to Solution.
It's best to assume your Custom Object list has no useful order to start with.
You give it an explicit order using SortTool, which anyone looking at your code will understand. SortTool allows you to sort on multiple fields, ascending and descending.
Remember to check the field(s) you want to sort on in the tree on the right-hand-side of Script Editor. (You always have to check off fields, of course, just pointing out that a field you sort on but don't output still must be checked.)
Here I'm checking using the Custom Object named EmailBound and checking the field with friendly name Created At in the tree:
The Velocity name of that field is createdAt (no spaces, different capitalization) so of course that's what I'll use in my code, not the friendly name.
I make sure to annotate the Velocity code to know exactly what objects and fields are required in the future.
#**
* @requires-objects
* EmailBound ($emailBound_cList)
* @requires-object-fields
* EmailBound.Created At (createdAt)
*#
Then call $sorter.sort to sort in descending order by createdAt, so the latest record is first in the list:
#**
* @requires-objects
* EmailBound ($emailBound_cList)
* @requires-object-fields
* CreatedAt ($emailBound_cList[].createdAt)
*#
#if( !$emailBound_cList.isEmpty() )
#set( $sortedEmailBound_cList = $sorter.sort($emailBound_cList,"createdAt:desc") )
Latest Created Object: ${sortedEmailBound_cList[0]}
#end
I've written extensively on Velocity, specifically Marketo's "flavor" of Velocity, so if you're going to be supporting Velocity code in production, I strongly suggest you read those blog posts.
Those are properties (dot-properties) of the object.
#set( $latestSortedObject = $sortedEmailBound_cList[0] )
Amount: ${latestSortedObject.Amount_c}
Any Other Field: ${latestSortedObject.OtherField_c}
It's best to assume your Custom Object list has no useful order to start with.
You give it an explicit order using SortTool, which anyone looking at your code will understand. SortTool allows you to sort on multiple fields, ascending and descending.
Remember to check the field(s) you want to sort on in the tree on the right-hand-side of Script Editor. (You always have to check off fields, of course, just pointing out that a field you sort on but don't output still must be checked.)
Here I'm checking using the Custom Object named EmailBound and checking the field with friendly name Created At in the tree:
The Velocity name of that field is createdAt (no spaces, different capitalization) so of course that's what I'll use in my code, not the friendly name.
I make sure to annotate the Velocity code to know exactly what objects and fields are required in the future.
#**
* @requires-objects
* EmailBound ($emailBound_cList)
* @requires-object-fields
* EmailBound.Created At (createdAt)
*#
Then call $sorter.sort to sort in descending order by createdAt, so the latest record is first in the list:
#**
* @requires-objects
* EmailBound ($emailBound_cList)
* @requires-object-fields
* CreatedAt ($emailBound_cList[].createdAt)
*#
#if( !$emailBound_cList.isEmpty() )
#set( $sortedEmailBound_cList = $sorter.sort($emailBound_cList,"createdAt:desc") )
Latest Created Object: ${sortedEmailBound_cList[0]}
#end
I've written extensively on Velocity, specifically Marketo's "flavor" of Velocity, so if you're going to be supporting Velocity code in production, I strongly suggest you read those blog posts.
Sanford,
Let me first say thanks for taking the time to look into this as you do with many other posts. It's greatly appreciated.
That code worked perfectly in retrieving the latest object.
My next hurdle is isolating a specific field to be returned. Right now I get everything that's checked.
For example, if I just want the Amount_c field from that latest object to be returned, what would that look like?
Life saver!!! Thank you.