SOLVED

Re: Velocity Script sorting on multiple characteristics

Go to solution
EllenSchwier
Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script sorting on multiple characteristics


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

 

 

View solution in original post

7 REPLIES 7
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script sorting on multiple characteristics


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 5

Re: Velocity Script sorting on multiple characteristics

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
Level 10 - Community Moderator

Re: Velocity Script sorting on multiple characteristics


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

 

 

EllenSchwier
Level 5

Re: Velocity Script sorting on multiple characteristics

Hi @SanfordWhiteman 

I was trying to test this script but kept getting errors (that I was working through with Marketo Support). But it appears to be an issue in the code and not in Marketo that does not allow me to test. Can you take a look at this?

#set( $filteredList = [] )
#foreach( $masterOrder_c in $masterOrder_cList )
#if( $masterOrder_c.status.equals("Cart") )
#set( $void = $filteredList.add($masterOrder_c) )
#end
#end
#set( $sortedList = $sorter.sort($filteredList,["hybrisUpdatedAt:desc"]) )
${sortedList.get(0).hTMLProductsList}

I am still new to Velocity Scripting, so I was not sure:

  • Is the order correct with the "end" in the correct place if I continue with the sorting after?
  • The part that seems to give issues in Marketo is the last line. Do I need to identify the custom object here?
  • ${sortedList.get(0).masterOrder_c.hTMLProductsList}

As always, any help is greatly appreciated!

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script sorting on multiple characteristics

The sorted and filtered lists have the exact same “shape” as the original list. They’re lists of the same objects (literally the same objects, not copies!) They may have the objects in a different order, or only hold a subset of the original list, but it’s all the same objects.

 

So filteredList contains only those masterOrder_c objects that have a particular status value Cart.

 

And sortedList[0] is the zero-th object in sortedList. That’ll be the masterOrder_c object with the greatest hybrisUpdatedAt value that also has status=Cart.

 

sortedList[0].hTMLProductsList would be the value of the property hTMLProductsList on the object sortedList[0].

EllenSchwier
Level 5

Re: Velocity Script sorting on multiple characteristics

Hi Sandford,

 

I am  getting a similar error to the one you helped fix in this post: https://nation.marketo.com/t5/product-discussions/error-with-velocity-token-sending-sample-email-ren...

 

I tried to use a similar approach. But now I always get an empty value. Is there another way to account for the possibility of an empty array?

 

#set( $filteredList = [] )
#foreach( $masterOrder_c in $masterOrder_cList )
#if( $masterOrder_c.status.equals("Cart") )
#set( $void = $filteredList.add($masterOrder_c) )
#end
#end
#set( $sortedList = $sorter.sort($filteredList,["hybrisUpdatedAt:desc"]) )
#if( !$display.alt($masterOrder_c.hTMLProductsList,"").isEmpty() )
${sortedList.get(0).hTMLProductsList}
#end

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script sorting on multiple characteristics


I tried to use a similar approach. But now I always get an empty value. Is there another way to account for the possibility of an empty array?

An empty array matches isEmpty(). You don’t have do do anything with $display.alt(). You use $display.alt() when there’s a string that may be null or empty. An empty array can’t be null.