Velocity Script - value range | Community
Skip to main content
Adele_Grono2
New Participant
August 6, 2018
Solved

Velocity Script - value range

  • August 6, 2018
  • 3 replies
  • 6605 views

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}

Sanford Whiteman

Best answer by SanfordWhiteman

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.

3 replies

Adele_Grono2
New Participant
August 7, 2018

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.

  1. #set( $colorsByCourseCode = {     
  2. "5428AB" : "blue",  
  3. "4410AS" : "green",   
  4. "4418PM" : "yellow",   
  5. "3409ES" : "red",   
  6. ".*" : "default text"     
  7. } )     
  8. #foreach( $coursePattern in $colorsByCourseCode.keySet() )     
  9. ## If we find a regex match for courseCode we're done     
  10. #if($lead.courseCode.matches("(?i)${coursePattern}") )     
  11.     #set( $color = $colorsByCourseCode[$coursePattern] )     
  12.     #break    
  13. #end     
  14. #end   
  15. $color 
SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
August 7, 2018

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.

Adele_Grono2
New Participant
August 7, 2018

Thank you so much Sanford Whiteman​, I selected "CourseCode from the tree, so the code now works. Thank you so so much, I did a screen shot so other people can also see. Thanks again

Adele_Grono2
New Participant
August 7, 2018

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​

SanfordWhiteman
New Participant
August 7, 2018

What's the field name, though? When you check off and drag that field onto the Script Editor canvas?

Adele_Grono2
New Participant
August 7, 2018

Hi there @Sanford Whiteman​


The field name is Course Code

REST API NameSOAP API NameFriendly Label
courseCodecourseCodeCourse Code

SanfordWhiteman
New Participant
August 6, 2018

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.