Velocity - Inserting last order id as a variable in emails | Community
Skip to main content
Anthony_Chong
Level 2
March 7, 2017
Solved

Velocity - Inserting last order id as a variable in emails

  • March 7, 2017
  • 2 replies
  • 3926 views

I am trying to use Velocity math tool to populate the last orderid for a customer. I can't quite seem to get the Velocity script to run properly.

When dragging the custom object variable, the following is inserted: ${jobItemList.get(0).jobItemId}

I do not understand what the get(0) is doing. Is it trying to pull a minimum value? First value? I want to pull the maximum value. I was trying to use a $math.max variable, but nothing seems to be working.

Any advice is greatly welcome! Thanks!

If anyone can help me in the Los Angeles area, let me know and we'll grab lunch!

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

Hi Anthony,

myList.get(0) is object-oriented longhand for myList[0], i.e. get the first item in the list. Using get() is not typically necessary and makes your code wordier, so I advise using the [] index notation instead.

If you want the item in the list with the highest jobItemId property, sort $jobItemList in descending order by jobItemId and get the first item. It is also good to have some guards in place in case you get an empty list.

#if( $jobItemList && $jobItemList.size() > 0 )

#set( $jobItemListHighestFirst = $sorter.sort($jobItemList,["jobItemId:desc"]) )

${jobItemListHighestFirst[0]}

#end

2 replies

SanfordWhiteman
SanfordWhitemanAccepted solution
Level 10
March 7, 2017

Hi Anthony,

myList.get(0) is object-oriented longhand for myList[0], i.e. get the first item in the list. Using get() is not typically necessary and makes your code wordier, so I advise using the [] index notation instead.

If you want the item in the list with the highest jobItemId property, sort $jobItemList in descending order by jobItemId and get the first item. It is also good to have some guards in place in case you get an empty list.

#if( $jobItemList && $jobItemList.size() > 0 )

#set( $jobItemListHighestFirst = $sorter.sort($jobItemList,["jobItemId:desc"]) )

${jobItemListHighestFirst[0]}

#end

Anthony_Chong
Level 2
March 7, 2017

Thank you so much Sanford! I'll give this a try.

Anthony_Chong
Level 2
March 15, 2017

Hi @Sanford Whiteman​! Thank you again for the above solution to my question.

I have a short follow up question if I may. The output generated looks something like "{jobItemId=1234567}". Is there any way for the output to only be the number?

Been playing with it and can't quite seem to get it.

Thank you again!

SanfordWhiteman
Level 10
March 15, 2017

"{jobItemId=1234567}". Is there any way for the output to only be the number?

That's the standard VTL object dump.

If you want one property of the object:

${jobItemListHighestFirst[0].jobItemId}