Hi there
The Velocity Script below selects the default value even if the record selected contains one of the values in the script.
Any advice on what code to change?
Many thanks
## Set up your map of xyz to related asset(s)
#set( $assetsBycourseCode = {
"5428AB" : {"IND" : "blue"},
"4410AS" : {"IND" : "green"},
"4418PM" : {"IND" : "yellow"},
"3409ES" : {"IND" : "red"},
"" : {
"IND" : "default text"
}
} )
##
## Start w/default asset set (empty key)
#set( $assets = $assetsBycourseCode[""] )
##
## Guard against bad inputs (null or empty object list)
#foreach( $assetSet in $assetsBycourseCode )
## If we find a regex match for [map key,CourseCode] we're done
#if($lead.courseCode.matches("(?i)${assetSet}") )
#set( $assets = $assetsBycourseCode[$assetSet] )
#break
#end
#end
${assets.IND}
Solved! Go to Solution.
On the right-hand-side of Script Editor, there's a tree of all the Person, Opportunity, and Custom Object fields. A field must be checked off there if it's going to be used in Velocity. For a very good reason -- memory conservation -- fields are not automatically exported into the Velocity context, they need to be manually selected.
What are you matching it against, though?
Right now this code will throw a fatal error because you're inserting unescaped braces into a regular expression.
i.e.
matches("(?i)${assetSet}")
when $assetSet is a hashmap (set of keys + values) is eval'd as
matches("(?i){IND=blue}")
which a bad regex.
Hi there Sanford Whiteman
"5428AB" is a code that is attached to the lead's record (its like a Product code or SKU) so if the lead contains this code "5428AB" then I would like the velocity script to insert the word "blue".
Then if the lead's record contains another code I would like the Velocity script to read all the product codes in the list.. for example if the lead record has the code, "4410AS" then I would like the velocity script to insert the word "green".
If the velocity script can't match the code on the lead's record to the values in the script I would like to insert the text "default text".
Sorry can you pls advise what the code change should be?
Many thanks for this Sanford Whiteman
What's the field name, though? When you check off and drag that field onto the Script Editor canvas?
Hi there Sanford Whiteman
The field name is Course Code
REST API Name | SOAP API Name | Friendly Label |
courseCode | courseCode | Course Code |
OK, I don't see the need for the extra hash in there, just:
#set( $colorsByCourseCode = {
"5428AB" : "blue",
"4410AS" : "green",
"4418PM" : "yellow",
"3409ES" : "red",
".*" : "default text"
} )
#foreach( $coursePattern in $colorsByCourseCode.keySet() )
## If we find a regex match for courseCode we're done
#if($lead.courseCode.matches("(?i)${coursePattern}") )
#set( $color = $colorsByCourseCode[$coursePattern] )
#break
#end
#end
$color
Hi there Sanford Whiteman
Sorry, still doesn't seem to be working, have attached a screen grab of the result.
DId you check off courseCode in the tree?
Hi there Sanford Whiteman - sorry I don't know what you mean by "Did you check off courseCode in the tree?" I just used the velocity script as per the below / as appears above.
On the right-hand-side of Script Editor, there's a tree of all the Person, Opportunity, and Custom Object fields. A field must be checked off there if it's going to be used in Velocity. For a very good reason -- memory conservation -- fields are not automatically exported into the Velocity context, they need to be manually selected.