Print this page

CONTAINS function use cases and examples

Knowledge Article Number 000004749
Resolution

The CONTAINS function compares 2 arguments and is commonly used in validation and workflow rules to search for a character or string in a text field. For more information on the CONTAINS function, read the documentation covering Formula Operators

 

CONTAINS usage

 

1. Search for text.
2. Check if an unknown string or character matches a defined set of strings or characters.

CONTAINS examples

Example of searching for text.
 

CONTAINS(Comments__c,"BadWord")
Returns TRUE if "BadWord" is found anywhere in Comments__c.
 

Example of searching for  unknown string or characters.
 

CONTAINS("0123456789", TextField__c)
Will return true for TextField__c values such as 1,2,9,01,789, or any other substring of "0123456789"

 
CONTAINS("0123456789", LEFT(TextField__c,1))
To only match numbers 0-9, the compare_text length must equal 1. In this case, the formula is checking to see if the first character of TextField__c is a number between 0-9.
 


When using CONTAINS to compare a string with a defined set the length of the elements of the set should match the length of the string being compared. However, it's still possible to match the delimiter. An equivalent CASE statement would be longer but more reliable.





promote demote