Velocity script retrieving fields value from last opportunity | Community
Skip to main content
Keyvan_HAMIDZAD
New Participant
April 11, 2018
Solved

Velocity script retrieving fields value from last opportunity

  • April 11, 2018
  • 1 reply
  • 4870 views

Hey guys!

I've been trying this Velocity script inside a token in order to retrieve fields value contained in the last opportunity attached to a lead:

$!OpportunityList.get(OpportunityList.size() - 1).fieldName

But an error occurred while rendering the email (error is happening when encountering " - 1").

Any thoughts about this?

Thanks a lot

Keyvan

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 Keyvan_HAMIDZAD

Ok I fixed it with this one:

#set($listSize = $OpportunityList.size() - 1)

$!OpportunityList.get($listSize).fieldName

The only case where it won't be working is when OpportunityList is empty (but this can't happen in my case).

Hope this helps!

1 reply

Keyvan_HAMIDZAD
Keyvan_HAMIDZADAuthorAccepted solution
New Participant
April 11, 2018

Ok I fixed it with this one:

#set($listSize = $OpportunityList.size() - 1)

$!OpportunityList.get($listSize).fieldName

The only case where it won't be working is when OpportunityList is empty (but this can't happen in my case).

Hope this helps!

SanfordWhiteman
New Participant
April 11, 2018

Hi Keyvan,

Math operators are technically unsupported by the VTL parser (which is why you'll see something like $variable-1 break while $variable - 1 with spaces works in certain cases). It's kind of dumb luck whether the syntax will make its way to the Java level intact.

So you should use the $math object instead. This is the last item in the ArrayList:

$Object__clist[$math.sub($Object__clist.size(),1)].fieldName

Adrian_Bivens
New Participant
April 22, 2021

Do you know if the objects in the Array are ordered by created date or modified date? For example, I want the values from the latest created object, yet with the snippet of code you provided, I'm getting the values of the last modified record. For our trigger campaigns it's not that big of a deal but we have use cases where older objects could be updated and throw a wrench in any batch campaigns.