Velocity Script sorting on multiple characteristics | Community
Skip to main content
EllenSchwier
Level 4
August 23, 2022
Solved

Velocity Script sorting on multiple characteristics

  • August 23, 2022
  • 1 reply
  • 3568 views

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.
Best answer by SanfordWhiteman

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

 

 

1 reply

SanfordWhiteman
Level 10
August 23, 2022

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.

 

EllenSchwier
Level 4
August 24, 2022

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)

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
August 24, 2022

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