Re: Velocity Scripting Question

aschwabe
Level 1

Velocity Scripting Question

I am trying to create a velocity script that will look at the Opportunity and the Opportunity Role in order to create a signature from the Opportunity Role. Here is what I have so far; however, when I preview my email, I'm getting an error message. Any suggestions are helpful! Thank you! 

 

#set($sort_oppty = $sorter.sort($OpportunityList, "MarketoUpdatedAt:desc"))
#foreach($oppty in $OpportunityList)
#if($oppty.OpportunityRole() == "Private Bank"
#set($AssociateId = $prod.associateID)
#break
#end
#end

#set($sort_oppty = $sorter.sort($OpportunityList, "MarketoUpdatedAt:desc"))
#set($SalesPersonTitleOverride = '')
#set($SalesPersonNameOverride = 'Company Name')
#set($SalesPersonPhNum = '')
#set($SalesPersonNMLS = '')
#set($SalesPersonEmail = '')
#set($SalesPersonCertification = '')
#set($SalesPersonPhotoUrl = Company Logo)

#foreach($oppty in $sort_oppty)
#if($oppty.associateID == $AssociateId)
#if(!(!$!oppty.advisorTitleOverride || $oppty.advisorTitleOverride.isEmpty()))
#set($SalesPersonTitleOverride = $oppty.advisorTitleOverride)
#end
#set($SalesPersonNameOverride = $oppty.advisorNameOverride)
#if(!(!$!oppty.advisorPhoneNumber || $oppty.advisorPhoneNumber.isEmpty()))
#set($PhNumLength = $oppty.advisorPhoneNumber.length())
#set($newPh1 = $oppty.advisorPhoneNumber.substring(1,4))
#set($newPh2 = $oppty.advisorPhoneNumber.substring(4,7))
#set($newPh3 = $oppty.advisorPhoneNumber.substring(7,$PhNumLength))
#set($SalesPersonPhNum = $newPh1 + "." + $newPh2 + "." + $newPh3)
#end
#if(!(!$!oppty.advisorNMLS || $oppty.advisorNMLS.isEmpty()))
#set($SalesPersonNMLS = "NMLS# " + $oppty.advisorNMLS)
#end
#set($SalesPersonEmail = $oppty.advisorEmail)
#set($SalesPersonBioLink = $oppty.advisorBioLink)
#set($SalesPersonCertification = $oppty.advisorCertification)
#if(!(!$!oppty.advisorPhotoUrl || $oppty.advisorPhotoUrl.isEmpty()))
#set($SalesPersonPhotoUrl = $oppty.advisorPhotoUrl)
#end
#break
#end
#end
3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Scripting Question

Please use the Syntax Highligher (“Insert/Edit Code Sample” on the toolbar) when posting code so it’s readable. I edited your post this time.

 

You say you’re getting “an error message” but didn’t say what that error was. Presumably you didn’t do much debugging, since there are fundamental syntax errors here: line 3 is missing a closing parenthesis, and line 16 (where you attempt to set $SalesPersonPhotoUrl) is missing quotes around the string. Either of those will cause Velocity to throw a fatal error.

 

As for the rest of the code: while it’s syntactically OK, it seems like you’re imagining certain approaches will work without testing them. $oppty.OpportunityRole is not a method, so you can’t call it like $oppty.OpportunityRole(). It’s gonna be a String. (When you try to call a nonexistent method, the line is just ignored. That’s what I mean by “syntactically valid”: it would be possible to call the method that way if it were a method.)

 

Also, why are you sorting the list twice on the same property?

 

And what is the $prod object?

 

Too many unanswered questions to go further, I’m afraid.

aschwabe
Level 1

Re: Velocity Scripting Question

Thank you for your response and I truly appreciate your help. Just to level-set, this is my first-time posting, I am not a developer and I am trying to revise the Velocity Script that we received from Marketo Professional Services to fit my use case. All that to say I am doing a lot of research and learning as quickly as possible so we can get an email out to our clients. I have found this article to be very helpful but isn't answering all of my questions: https://nation.marketo.com/t5/champion-program-blogs/upping-your-token-game-getting-started-with-vel...

 

I realized that I made a few mistakes in the draft that I shared and since have updated the first 4 lines to look at the opportunity vs. the product object (which is a custom object that isn't relevant here). 

 

Everything after the first 4 lines was provided by Marketo Premier Services for another project but is still applicable to this project. 

 

I am trying to pull the opportunity role = private bank through to the signature line of the email. 

 

aschwabe_0-1721051792158.png

 

#if($oppty.Role.equals("Private Bank"))
#set($AssociateId = $oppty.associateID)
#break
#end

#set($sort_oppty = $sorter.sort($OpportunityList, "MarketoUpdatedAt:desc"))
#set($SalesPersonTitleOverride = '')
#set($SalesPersonNameOverride = 'Company Name')
#set($SalesPersonPhNum = '')
#set($SalesPersonNMLS = '')
#set($SalesPersonEmail = '')
#set($SalesPersonCertification = '')
#set($SalesPersonPhotoUrl = Company Logo)

#foreach($oppty in $sort_oppty)
#if($oppty.associateID == $AssociateId)
#if(!(!$!oppty.advisorTitleOverride || $oppty.advisorTitleOverride.isEmpty()))
#set($SalesPersonTitleOverride = $oppty.advisorTitleOverride)
#end
#set($SalesPersonNameOverride = $oppty.advisorNameOverride)
#if(!(!$!oppty.advisorPhoneNumber || $oppty.advisorPhoneNumber.isEmpty()))
#set($PhNumLength = $oppty.advisorPhoneNumber.length())
#set($newPh1 = $oppty.advisorPhoneNumber.substring(1,4))
#set($newPh2 = $oppty.advisorPhoneNumber.substring(4,7))
#set($newPh3 = $oppty.advisorPhoneNumber.substring(7,$PhNumLength))
#set($SalesPersonPhNum = $newPh1 + "." + $newPh2 + "." + $newPh3)
#end
#if(!(!$!oppty.advisorNMLS || $oppty.advisorNMLS.isEmpty()))
#set($SalesPersonNMLS = "NMLS# " + $oppty.advisorNMLS)
#end
#set($SalesPersonEmail = $oppty.advisorEmail)
#set($SalesPersonBioLink = $oppty.advisorBioLink)
#set($SalesPersonCertification = $oppty.advisorCertification)
#if(!(!$!oppty.advisorPhotoUrl || $oppty.advisorPhotoUrl.isEmpty()))
#set($SalesPersonPhotoUrl = $oppty.advisorPhotoUrl)
#end
#break
#end
#end

 

Please let me know if this is helpful and if you need more information. Thank you!

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Scripting Question

There’s nothing that creates an object called $oppty before this code:

 

#if($oppty.Role.equals("Private Bank"))
#set($AssociateId = $oppty.associateID)
#break
#end

 

Therefore the #if condition will never match and the global variable $AssociateId will always be null.

 

Then this code loops over the Opportunities and attempts to match $AssociateId to the Opportunity property associateId, but it will never match unless they’re both null:

 

#foreach($oppty in $sort_oppty)
#if($oppty.associateID == $AssociateId)

 

 

Everything after the first 4 lines was provided by Marketo Premier Services for another project but is still applicable to this project.

Applicable it may be, but must say it was badly written in the first place and that makes troubleshooting very difficult.

 

Take this line:

 

#if(!(!$!oppty.advisorTitleOverride || $oppty.advisorTitleOverride.isEmpty()))

 

 

A far more readable way to determine if something is not null and not empty is:

 

#if( !$display.alt($something,"").isEmpty() )