SOLVED

Re: Velocity Script - value range

Go to solution
Adele_Grono2
Level 2

Velocity Script - value range

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

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - value range

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.

View solution in original post

10 REPLIES 10
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - value range

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.

Adele_Grono2
Level 2

Re: Velocity Script - value range

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
Level 10 - Community Moderator

Re: Velocity Script - value range

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

Adele_Grono2
Level 2

Re: Velocity Script - value range

Hi there Sanford Whiteman


The field name is Course Code

REST API NameSOAP API NameFriendly Label
courseCodecourseCodeCourse Code

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - value range

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

Adele_Grono2
Level 2

Re: Velocity Script - value range

Hi there Sanford Whiteman

Sorry, still doesn't seem to be working, have attached a screen grab of the result.

screen grab of velocity script result.png

SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity Script - value range

DId you check off courseCode in the tree?

Adele_Grono2
Level 2

Re: Velocity Script - value range

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
Level 10 - Community Moderator

Re: Velocity Script - value range

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.