Re: How do you access the most recent item in a custom object?

Anonymous
Not applicable

How do you access the most recent item in a custom object?

We use custom objects to store data pertaining to our customers' homes.

Since a customer could own more than one home, the result is multiple items (or homes) in the "propertyDetails" custom object.

Whenever I use scripted email tokens to populate an email with this data, it uses the oldest item.

Here's the Velocity script that Marketo uses by default:

${propertyDetails_cList.get(0).streetAddress}

Can this script be changed to reference the most recent item's Street Address?

Thanks in advance!

12 REPLIES 12
SanfordWhiteman
Level 10 - Community Moderator

Re: How do you access the most recent item in a custom object?

See here: https://blog.teknkl.com/tip-switch-trigger-and-batch-object/

And also, if you're getting your feet wet with Velocity, all of these: https://blog.teknkl.com/tag/velocity/

Anonymous
Not applicable

Re: How do you access the most recent item in a custom object?

Hi Sanford,

Thanks for your response. I'm not 100% sure how your blog post applies to my situation, or what to take away from it.

Would you mind explaining how this can be accomplished?

SanfordWhiteman
Level 10 - Community Moderator

Re: How do you access the most recent item in a custom object?

The post explains how to sort the list, descending, by a datetime field (a field that's on every record, like updatedAt or any other field you want) and take the first item in the  sorted list.

That's exactly what gets you "the most recent item" in Velocity.

Anonymous
Not applicable

Re: How do you access the most recent item in a custom object?

This solution would work for batch campaigns, but not so much triggered since it limits you to a specific date and is not evergreen.

Does that make sense? Essentially I need to select the newest item, regardless of the date.

SanfordWhiteman
Level 10 - Community Moderator

Re: How do you access the most recent item in a custom object?

When you sort a list by a date field, descending, the first item is always the latest date.

There's no "specific date" involved. Don't know where you're getting that idea. The code in the post doesn't have a static date value in it.

Anonymous
Not applicable

Re: How do you access the most recent item in a custom object?

You're 100% right. Sorry for missing that!

So if I wanted to populate an email script token with the Seller Address field from the most recent propertyDetails custom object item, what would the script look like?

Sorry for not taking the time to learn Velocity myself and asking you to provide the script. I do plan on learning Velocity, but need to figure out a solution for this roadblock asap.

Thanks again for your assistance.

SanfordWhiteman
Level 10 - Community Moderator

Re: How do you access the most recent item in a custom object?

#set( $propertyDetailsByMostRecent = $sorter.sort($propertyDetails_cList,"updatedAt:desc") )

#set( $latestPropertyDetails = $propertyDetailsByMostRecent[0] )

The street address of the latest property is ${latestPropertyDetails.streetAddress}

Both fields Street Address and Updated At need to be checked off in the tree in Script Editor.

Maria_Riley
Level 1

Re: How do you access the most recent item in a custom object?

Hi Sanford, 

We are using the formula above successfully, bringing the last product for a Membership. 

#set( $MembershipMostRecent = $sorter.sort($Membership__cList,"CreatedDate:desc") )
#set( $LatestMembershiDdetails = $MembershipMostRecen[0] )
The product of the latest membership is ${LatestMembershiDdetails.Product_Name__c}

Now we would like to bring the latest price which only sits on the Membership>>renewal. 

How do you reference Membership renewal list?

Marketo Price Field

${Membership__cList.get(0).Membership_Renewal__cList.get(0).Sales_Price__c}

thanks for your help, 

Maria Riley

Grace_Brebner3
Level 10

Re: How do you access the most recent item in a custom object?

Hey Maria,

You should open a new question for this rather than replying to a ten month old thread - you're more likely to get input, plus you'll be helping others in community with similar questions (it'll be more readily found in searches & you'll be able to mark answers as correct)