SOLVED

Velocity - Inserting last order id as a variable in emails

Go to solution
Anthony_Chong
Level 1

Velocity - Inserting last order id as a variable in emails

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Inserting last order id as a variable in emails

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

View solution in original post

7 REPLIES 7
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Inserting last order id as a variable in emails

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 1

Re: Velocity - Inserting last order id as a variable in emails

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

Anonymous
Not applicable

Re: Velocity - Inserting last order id as a variable in emails

Hi Sanford,

This is a slightly different issue but I wanted to see if the answer to this question might apply to what I'm dealing with as well.

Basically, I'm trying to send out appointment confirmation emails, based on data in a custom object. It's possible for customers to have more than one appointment in the object. We're getting notifications that customers are receiving old information, and it indeed looks like the tokens are not grabbing the most recent items in the object.

How would I get each token (each based on a field within the custom object) to display the most recent information? I tried reusing the answer you put in above, as it seems to be what I'm looking for, but it still spit out the old information at me (plus I think I formatted the display wrong).

Here's an example of the code we're using to show an appointment date:

## find closest future LLS appointment

## begin selection logic

#set($matchedAppt = false)

#foreach($appt in ${appointment_cList})

#set($matchedApptDate = false)

#set($currentApptDate = $date.toDate('yyyy-M-d', $appt.appointmentDate))

#set($currentApptOffset = $date.difference($date, $currentApptDate).getMilliseconds())

## if appointment is in future

#if($currentApptOffset > 0 && $appt.orderType == 'LLS')

## first match

#if(! $matchedAppt)

#set($matchedAppt = $appt)

## nth match

#elseif($matchedAppt)

#set($matchedApptDate = $date.toDate('yyyy-M-d', $matchedAppt.appointmentDate))

#end

##endif match

## if comparable

#if($currentApptDate && $matchedApptDate)

#set($difference = $date.difference($currentApptDate, $matchedApptDate).getMilliseconds())

## current closer than existing match

#if($difference > 0)

#set($matchedAppt = $appt)

#end

#end

##endif comparable

#end

##endif future

#end

##endfor

## end selection logic

## begin usage

#if($matchedAppt && $matchedAppt.appointmentDate)

<span>$date.format('full_date',$date.toDate('yyyy-M-d', $matchedAppt.appointmentDate))</span>##

#else

<span>Unknown</span>##

#end

In this case, it just spits out 'Unknown' at me in the test emails. The code here was done by a 3rd party and I'm wondering if it's wrong to begin with. I just want it to select the most recent entry. Any advice?

Thanks so much!

-Alex

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity - Inserting last order id as a variable in emails

Why don't you open a new thread so we don't confuse Correct answers?

Also, are your date strings really yyyy-M-d (2016-3-4) and not yyyy-MM-dd (2016-03-04)?

Anonymous
Not applicable

Re: Velocity - Inserting last order id as a variable in emails

Hi Sanford, will do.

The date strings look like that in the code, but they output as yyyy-mm-dd. I'll include more info in the new post.

Thanks again.

Anthony_Chong
Level 1

Re: Velocity - Inserting last order id as a variable in emails

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

Re: Velocity - Inserting last order id as a variable in emails

"{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}