Re: More than one Opportunity per Contact - Velocity Script

Scott_Springer
Level 1

More than one Opportunity per Contact - Velocity Script

Dear Marketo Community,

We use tokens to generate notification emails for appointment bookings, which pulls from the Opportunities field mapping.

Our issue is that often a contact has multiple opportunities listed against it, and the current scripting logic we have is always pulling the "latest" opportunity linked to the contact - I believe this is associated with the get(0) function in the Velocity script.

The end result is that our customers are getting notifications for appointment bookings which is pulling reference information that is from the wrong opportunity (usually due to a cancelled or rescheduled booking)

Token examples

${display.alt($OpportunityList.get(0).sourceJobNumber,"")} 
${OpportunityList.get(0).Stage}

Is anyone aware of how to add a qualifier to the velocity script such that we can refine the criteria of how the token is being populated? We don't necessarily want the "latest", we want to specify criteria on how to match the Opportunity needed, in our case show the Job Number which has Opportunity Stage = "Confirmed". I imagine it's a combination of specifying this in the velocity script, but can't work it out.

Thanks for any pointers or suggested solutions for this challenge, we were thinking it may be a relatively common challenge, but could not find any mention of it.

Many thanks,

Scott

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: More than one Opportunity per Contact - Velocity Script

Loop over the list of Opportunities, compare the property value. Search for my posts that include  the #foreach operator -- there are tons of examples of matching items in the list.

SanfordWhiteman
Level 10 - Community Moderator

Re: More than one Opportunity per Contact - Velocity Script

Also pls highlight your code using the Syntax Highlighter (choose Java from the drop-down as it's closest to Velocity).

Scott_Springer
Level 1

Re: More than one Opportunity per Contact - Velocity Script

Weird, we received some helpful comments from Sanford for this post, then I edited the original post, but his comments disappeared, maybe a moderators approval step involved. I'll say thank you here, it worked, and I will post the solution here.

Scott_Springer
Level 1

Re: More than one Opportunity per Contact - Velocity Script

Just sharing our implementation, it was a relatively simple coding solution now that we see how #foreach works. I'll say it again, thanks Sanford Whiteman

#foreach($list in ${OpportunityList})

#if($list.Stage=="Booked")
<html>
<body>
Job Reference Number: $list.sourceJobNumber </br>
Car Registration Number: $list.vehicleRego</br>
Date: $list.appointmentWindowStartDayText</br>
Time: $list.appointmentWindowStartTimeText</br>
Address: $list.serviceAddress</br></br></br>
</body>
</html>
#end
#end
SanfordWhiteman
Level 10 - Community Moderator

Re: More than one Opportunity per Contact - Velocity Script

Thanks for sharing, Scott! Two things though...

  • should be using $OpportunityList, not ${OpportunityList}; otherwise, syntax errors are inevitable as you build out the code
  • use $list.stage.equals("Booked"), not the == operator, since == is buggy (there absolutely must be a blog post on this, I just have so many other things to do!)
Scott_Springer
Level 1

Re: More than one Opportunity per Contact - Velocity Script

Great to note, we'll follow this recommendation, many thanks.