Email Script - contains one of multiple values | Community
Skip to main content
Travis_Wittenbu
New Participant
May 6, 2021
Solved

Email Script - contains one of multiple values

  • May 6, 2021
  • 1 reply
  • 1257 views

Check out this script. It currently looks in our 'lead.Subscribed_Products_From_Account__c' field for various words and if it finds a match, outputs a link, if it doesn't find a match outputs a different link.

 

#if( $lead.Subscribed_Products_From_Account__c.contains("Alpha")) <b><a href="https://mysite.com">https://mysite.com</a></b>* #elseif( $lead.Subscribed_Products_From_Account__c.contains("Beta")) <b><a href="https://mysite.com">https://mysite.com</a></b>* #elseif( $lead.Subscribed_Products_From_Account__c.contains("Gamma")) <b><a href="https://mysite.com">https://mysite.com</a></b>* #else <b><a href="https://NOTmysite.com">https://NOTmysite.com</a></b>* #end

 

 

How can I combine the first 3 rules into a single rule?  I have tried the option below but it does not work - 

 

#if( $lead.Subscribed_Products_From_Account__c.contains("Alpha","Beta","Gamma")) <b><a href="https://mysite.com">https://mysite.com</a></b>* #else <b><a href="https://NOTmysite.com">https://NOTmysite.com</a></b>* #end

 

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

String.contains() only takes a single argument, a single string. Any other arguments cause the code to throw an error, which Velocity treats as a null result.

 

To check for multiple substrings in a string, use a regular expression and String.matches().

#if( $lead.Subscribed_Products_From_Account__c.matches(".*(Alpha|Beta|Gamma).*") )

 

Is this value you're searching actually a delimited string, though? Because you shouldn't just be scanning left-to-right it if so. You should split it on the delimiter (creating an array) then see if the array contains any of your interesting values.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
May 6, 2021

String.contains() only takes a single argument, a single string. Any other arguments cause the code to throw an error, which Velocity treats as a null result.

 

To check for multiple substrings in a string, use a regular expression and String.matches().

#if( $lead.Subscribed_Products_From_Account__c.matches(".*(Alpha|Beta|Gamma).*") )

 

Is this value you're searching actually a delimited string, though? Because you shouldn't just be scanning left-to-right it if so. You should split it on the delimiter (creating an array) then see if the array contains any of your interesting values.