Salesforce: Formula with CONTAINS() function
The CONTAINS function is mostly used in validation and workflow rules to search for a character or string in a text field.
- CONTAINS will return TRUE if “compare_text” is found in “text” and FALSE if not.
- The comparison is case sensitive.
- When using CONTAINS function in a validation rule, fields that are blank are considered as valid in Salesforce. Like if you have a validation rule that tests to see if the serial number of an asset contains “A,” all assets that have a blank serial number are considered valid.
- USE CASES for this function
1.Searching for Text
Example 1: Like we have a custom field Comments__c on Contact and we don’t want user to enter Bad Word into Comments section . So here we can use this validation rule for prevent user to enter Bad data.
- Returns TRUE if “BadWord” is found anywhere in Comments__c.
2. Check if an unknown string or character matches a defined set of strings or characters.
Example 2: Like we
Will return TRUE if Country is INDIA,JAPAN,US,UK or any exact match of “INDIA,JAPAN,US,UK”.
- Before some days ago some user asked question on Success Community for Contains validation rule. User wants to save her records only when his custom field contain some specific word . Like we have one custom field My_Field__c and we want that user is able to save this only it will contain “Salesforce” otherwise it will prevent user’s from saving the record.
IF((CONTAINS( My_Field__c,”Salesforce”)), false, true)
So it will verify here that T My_Field__c field having this Salesforce word then only it will save the record.