Skip to main content
Keyvan_HAMIDZAD
Level 2
Apr 11, 2018 10:05 AM UTC
Solved

Velocity script retrieving fields value from last opportunity

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.
Keyvan_HAMIDZAD
Keyvan_HAMIDZADAccepted solution
Level 2
Apr 11, 2018 12:28 PM UTC

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!

6 replies

Keyvan_HAMIDZAD
Keyvan_HAMIDZADAuthorAccepted solution
Level 2
Apr 11, 2018 12:28 PM UTC

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!

Translate
SanfordWhiteman
User Group Leader
User Group Leader
Apr 11, 2018 6:16 PM UTC

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

Translate
Adrian_Bivens
Level 2
Apr 22, 2021 6:10 PM UTC

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.

Translate