Using multiple contains values in Velocity | Community
Skip to main content
New Participant
August 8, 2018
Solved

Using multiple contains values in Velocity

  • August 8, 2018
  • 1 reply
  • 8428 views

This is my first time using Velocity so apologies if it's an obvious answer. We need to populate based on multiple contains values in a single field, and have a default for anything that doesn't match but I'm not sure how best to write this out using Velocity. How would I write out this script?

If Job Function contains "programmer" "engineer"

Show: "Hello techie person"

elseIf Job Function contains "marketer" "sales"
Show: "Hello marketing person"

elseIf Job Function doesn't contain any of those values

Show: "Hello general person"

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

Thank you! I keep wondering if it would be faster to set up individual email script tokens with the tables of each images (the team keeps complicating this with more formatting needs, size changes, adding text, links, etc), but I'm still figuring out how to best write this all out so I really appreciate the guidance!


#set( $imagePath = "http://lp.accessintel.com/rs/881-ZTT-725/images/" )         

#set( $outputByjobFunction = [           

{   

  "pattern" : "(?i).*\b(programmer|engineer)\b.*",    

  "color" : "#800080",   

  "greeting" : "techie person",  

  "images" : [

    "sm-black_i_50.gif", 

    "sm-black_f_50.gif", 

    "sm-black_t_50.gif" 

  ]

},   

{   

  "pattern" : "(?i).*\b(marketer|sales)\b.*",    

  "color" : "#B1309D",   

  "greeting" : "marketing person", 

  "images" : [

    "sm-rnd_fb_grey.png", 

    "sm-rnd_i_grey.png"   

  ]

},   

{   

  "pattern" : ".*",    

  "color" : "#99cc00",   

  "greeting" : "general person", 

  "images" : [

    "sm-square_li_100.gif", 

    "sm-square_g_100.gif", 

    "sm-square_fb_100.gif",  

    "sm-square_t_100.gif"  

  ]

}   

] )          

#foreach( $outputSet in $outputByjobFunction )          

#if( $lead.jobFunction.matches($outputSet.pattern) )         

    #set( $matchedOutputSet = $outputSet )  

    #break         

#end          

#end   

<span style="color: ${matchedOutputSet.color};">${matchedOutputSet.greeting}</span>   

  <table width="60%" border="0" cellspacing="0" cellpadding="6"> 

  <tbody> 

    <tr>

    #foreach( $image in $matchedOutputSet.images ) 

      <td align="center"><img src="${imagePath}${image}" width="30"></td> 

    #end

    </tr> 

  </tbody> 

</table>

I keep wondering if it would be faster to set up individual email script tokens with the tables of each images (the team keeps complicating this with more formatting needs, size changes, adding text, links, etc)

It's almost never better to repeat yourself, though at the same time you have to stop people from constantly changing the spec on you...

1 reply

SanfordWhiteman
New Participant
August 8, 2018

Have you searched past threads on Velocity topics? There was one just the other day with a similar aim (the function is matches(), not contains ()).

New Participant
August 8, 2018

Hi Sanford,

Are you referring to this discussion? Velocity Script - value range

It's helpful in showing how to set up a consolidated list of matches but I'm a little confused about how I can use this to:

1. Target on incomplete values (so the lead's Job Function could be "programmer of nightmares" and they'd be included when I specify "programmer"

2. How to target multiple incomplete values in the same string

3. Use html as the shown content
4. How to test the default value

I'm not sure if this helps as an example, but here's what I'm attempting to do, and probably wrote out very badly

#set( $textByjobFunction = {     

"programmer" "engineer" : "<span style="color: #800080;">Hello techie person</span>", 

"marketer" "sales" : "<span style="font-size: 16px; color: #B1309D;">Hello marketing person</span>",    

"*" : "<span style="font-size: 16px; color: #99cc00;">Hello general person</span>"    

} )    

#foreach( $jobFunctionValue in $textByjobFunction.keySet() )    

## If we find a regex match for jobFunction we're done    

#if($lead.jobFunction.matches("(?i)${jobFunctionValue}") )    

    #set( $text = $htmlByjobFunction[$jobFunctionValue] )    

    #break   

#end    

#end  

$text

SanfordWhiteman
New Participant
August 8, 2018

Well, that isn't valid VTL to begin with so it will always throw an error, even before you get to any business logic.

You also don't want to repeat yourself; the only thing different about those SPANs is the color so it should be factored out.

Something more like this:

#set( $outputByjobFunction = [       

{

  "pattern" : "(?i).*\b(programmer|engineer)\b.*",

  "color" : "#800080",

  "greeting" : "techie person"

},

{

  "pattern" : "(?i).*\b(marketer|sales)\b.*",

  "color" : "#B1309D",

  "greeting" : "marketing person"

},

{

  "pattern" : ".*",

  "color" : "#99cc00",

  "greeting" : "general person"

}

] )      

#foreach( $outputSet in $outputByjobFunction )      

#if( $lead.jobFunction.matches($outputSet.pattern) )     

    #set( $matchedOutputSet = $outputSet )      

    #break     

#end      

#end

<span style="color: ${matchedOutputSet.color};">${matchedOutputSet.greeting}</span>

I did just write this on a plane so it may not be perfect, but it is the right pattern to follow.