SOLVED

Velocity script retrieving fields value from last opportunity

Go to solution
Keyvan_HAMIDZAD
Level 1

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

1 ACCEPTED SOLUTION

Accepted Solutions
Keyvan_HAMIDZAD
Level 1

Re: Velocity script retrieving fields value from last opportunity

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!

View solution in original post

6 REPLIES 6
Keyvan_HAMIDZAD
Level 1

Re: Velocity script retrieving fields value from last opportunity

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!

View solution in original post

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script retrieving fields value from last opportunity

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
Level 1

Re: Velocity script retrieving fields value from last opportunity

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.

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script retrieving fields value from last opportunity

You should assume the objects in a list are not ordered and sort them yourself using $sorter.sort if the order is important. You'll have full control over ascending/descending and can sort on multiple fields.

Adrian_Bivens
Level 1

Re: Velocity script retrieving fields value from last opportunity

Now I'm lost. I thought the previous request was regarding retrieving values from the latest object created in Salesforce.

This is what I have now based on what I've found online, the problem is the values returned aren't consistent. I just need the latest record's values.

 

${number.format("${esc.d}${esc.h},${esc.h}${esc.h}${esc.h}", ${Transaction_Queue__cList[$math.sub($Transaction_Queue__cList.size(),1)].Amount__c})}
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity script retrieving fields value from last opportunity

Continued on your new thread.