Which opportunity does an opp-email scripting token pull from? | Community
Skip to main content
New Participant
August 16, 2018
Solved

Which opportunity does an opp-email scripting token pull from?

  • August 16, 2018
  • 1 reply
  • 4385 views

Hi all,

So we know that a person can have more than one opportunity, and that not everyone has the same amount of opportunities. When creating a velocity scripting token to pull from the opportunity, how do I tell it to pull from the most recent opportunity?

- I am trying to pull the opportunity owner's name and information for the signature of an email.

$OpportunityList.get(0).Oppty_Owner_License__c

I believe the .get(0) references the opportunity but which one!?

Any help is much appreciated!!

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 SanfordWhiteman

$someList.get(0)

is the same as

$someList[0]

They both get the first element in the list, in insertion order. (List indexes start from 0.)

If you want to get the first element in the list according to some other criteria, first sort the list, for example sorting it by a date property in descending order:

#set( $sortedList = $sorter.sort($someList, "createdAt:desc") )

Now

$sortedList[0]

will be the "latest" element.

Note you need to have createdAt checked off in the tree in Script Editor in order to sort by that property.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
August 16, 2018

$someList.get(0)

is the same as

$someList[0]

They both get the first element in the list, in insertion order. (List indexes start from 0.)

If you want to get the first element in the list according to some other criteria, first sort the list, for example sorting it by a date property in descending order:

#set( $sortedList = $sorter.sort($someList, "createdAt:desc") )

Now

$sortedList[0]

will be the "latest" element.

Note you need to have createdAt checked off in the tree in Script Editor in order to sort by that property.

ErinCa12Author
New Participant
August 16, 2018

Thanks Sanford,

I took what you said and also incorporated learnings from your blog https://blog.teknkl.com/sorting-objects-and-lists-in-velocity/  to sort with more than one element.

Here is my final version for others to learn from:

#set( $sortedList = $sorter.sort( $OpportunityList,["IsClosed:asc","ExternalCreatedDate:desc"]))

$OpportunityList[0].Opportunity_Owner_Mobile_Phone__c

ErinCa12Author
New Participant
August 16, 2018

Sanford,

How can I wrap the last statement in the .isEmpty clause? $OpportunityList[0].Opportunity_Owner_Mobile_Phone__c is populating in the email when the field is bland. I can't seem to find the right statement.

#if( !$OpportunityList[0].Opportunity_Owner_Mobile_Phone__c.isEmpty() )

#set( $sortedList = $sorter.sort( $OpportunityList,["IsClosed:asc","ExternalCreatedDate:desc"]))

$OpportunityList[0].Opportunity_Owner_Mobile_Phone__c

#end