Velocity Script sorting on multiple characteristics | Community
Skip to main content
EllenSchwier
Level 4
Aug 23, 2022 12:19 PM UTC
Solved

Velocity Script sorting on multiple characteristics

I am trying to write a Velocity Script that will populate a field from a custom object into an email. The logic in the script needs to identify the correct custom object to use. Specifically, I want the most recently updated record with a specific status. I tried using this code, but it did not always identify the correct custom object.

 

 

#set( $sortedList = $sorter.sort($masterOrder_cList,["status:asc","UpdatedAt:desc"]) ) ${sortedList.get(0).COFieldName}

 

 

Any tips are appreciated. 

 

What I really want to do is create a work around for the fact that trigger tokens do not work with the Custom Object is Updated trigger....so if you have any ideas on that front, I would also be interested.

 

Thank you!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Aug 24, 2022 7:35 PM UTC

Thank you for responding, Sandford! My status options are "cart", "quote" and "order" so my initial code was looking for "carts" which I could do simply by sorting alphabetically on status.

Hmm, sort of haphazard way of getting the data — not that I haven’t done it myself! But it’s too dependent on alpha sorting.

 

The better way is to create a new ArrayList and copy matching objects into that list:

#set( $filteredList = [] ) #foreach( $object in $originalList ) #if( $somecriteria.equals(true) ) #set( $void = $filteredList.add($object) ) #end #end

 

 

7 replies

SanfordWhiteman
Level 10
Aug 23, 2022 4:05 PM UTC

Specifically, I want the most recently updated record with a specific status. I tried using this code, but it did not always identify the correct custom object.#set( $sortedList = $sorter.sort($masterOrder_cList,["status:asc","UpdatedAt:desc"]) ) ${sortedList.get(0).COFieldName}

It’s not clear what you mean by “with a specific status”. Your code is correct if you want to sort by a field called status, then by a field called UpdatedAt.  (Note the standard field in Custom Objects is updatedAt with a lowercase “u”, so this is kind of surprising.)

 

The code is not filtering the list to only reveal objects with one particular status.

 

Translate
EllenSchwier
Level 4
Aug 24, 2022 7:38 AM UTC

Thank you for responding, Sandford! My status options are "cart", "quote" and "order" so my initial code was looking for "carts" which I could do simply by sorting alphabetically on status.

 

However, if you have tips/examples of how I can filter on status, and then select the most recently updated...that would be even better. Or can I also filter on only those updated in the last day?

 

(And to answer your other comment, I have a custom UpdatedAt field, but tried to make it generic for posting here)

Translate
SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
Aug 24, 2022 7:35 PM UTC

Thank you for responding, Sandford! My status options are "cart", "quote" and "order" so my initial code was looking for "carts" which I could do simply by sorting alphabetically on status.

Hmm, sort of haphazard way of getting the data — not that I haven’t done it myself! But it’s too dependent on alpha sorting.

 

The better way is to create a new ArrayList and copy matching objects into that list:

#set( $filteredList = [] ) #foreach( $object in $originalList ) #if( $somecriteria.equals(true) ) #set( $void = $filteredList.add($object) ) #end #end

 

 

Translate