7 Replies Latest reply on Mar 15, 2017 8:14 AM by Alex Bridges

    Velocity - Inserting last order id as a variable in emails

    Anthony Chong

      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!

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

          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
          
          1 of 1 people found this helpful
            • Re: Velocity - Inserting last order id as a variable in emails
              Anthony Chong

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

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

                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

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

                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!