CONTAINS() Function in Salesforce | Syntax, Examples, & Use Cases

While working at a company that uses Salesforce for customer relationship management, I needed to check whether a text field contained a specific substring.

As a solution, I found that Salesforce provides a function called CONTAINS that checks whether a specific text value exists within another text field.

In this tutorial, we will learn how to use the CONTAINS() function in Salesforce. We will also learn real-world business examples using the CONTAINS function syntax.

If you want to learn how to use the CONTAINS function in the formula field in Salesforce, let’s dive deep into the complete article and learn.

What is the CONTAINS() Function in Salesforce?

The Salesforce CONTAINS() function is one of the Text() Functions used to perform text operations. The CONTAINS function checks whether the text field contains the specified substring.

In addition to this, the CONTAINS function is case-sensitive, which means it knows the difference between uppercase and lowercase characters. And we can use this function in validation and workflow rules.

Moreover, you have to keep one thing in mind while working with this function: the passed text field must be of the TEXT field type. If you pass any other field type as a parameter to the function, you will get an error.

And if you are passing the simple text to the function as a parameter of the text field, make sure it is enclosed in single or double quotation marks.

Furthermore, this CONTAINS function returns a Boolean value (True or False) depending on whether the string passed matches the text field.

Syntax and Usage of the CONTAINS Function in Salesforce

Below is the syntax of the CONTAINS function in Salesforce:

CONTAINS(text_field,substring)
  • text_field: This text_field parameter specifies the text field for which you want to check the presence of the substring.
  • substring: This substring parameter specifies the substring that you want to find within the text field.

Here is an illustration of how to use a formula field’s “CONTAINS” function. Suppose we want to categorize leads’ cases based on keywords such as “Prospect”“Inquiry”, or “Interested”.

Here is the formula:

IF(
    CONTAINS(Description, "refund"),
    "Refund Case",
    IF(
        CONTAINS(Description, "complaint"),
        "Complaint Case",
        IF(
            CONTAINS(Description, "shipping"),
            "Shipping Case",
            "Other Case"
        )
    )
)

With this, we have learned the syntax of the CONTAINS function with an example. And now, we will proceed and learn how to use the CONTAINS function in Salesforce Lightning.

How to use the CONTAINS() Function in Salesforce

Below are the steps to use the CONTAINS function in Salesforce Lightning:

  1. Open Salesforce Lightning mode. Next, click the “Gear Icon” in the top right corner of the page. Click Setup from the dropdown menu to continue.
CONTAINS Function in Salesforce
  1. The setup page opens when you select the Setup option. Here, the “Object Manager” is found in the Navigation Bar next to the Home tab. Click on it.
CONTAINS Function in Salesforce Example
  1. The object manager page opens when you click it. There are numerous objects seen here. Use the Quick Find search field to find the item on which we want to use the CONTAINS() function.
    • Now, click the “Lead” object to move to the lead object page.
CONTAINS Function in Salesforce Lightning
  1. On the product object page, the “Field & Relationship” option is in the details section on the left. Select “Field & Relationship” from the menu.
    • The field and relationships page is opened after selecting the field and relationship option. Click the “New” button at the top of the page.
CONTAINS Function in Salesforce Lightning Example
  1. The field type page opens when you click the new button. The page contains a wide variety of field types. In this case, I chose the “Formula” field type, as I want to use the TEXT function in the advanced formula.
    • Click the “Next” button to move on to the next step.
Salesforce Lightning CONTAINS Function Example
  1. The choose output type screen appears after clicking the next button. Enter the information now by filling out the “Field Label” and the “Field Name”, which is automatically filled in when you put the cursor on the field name. In this instance, I typed “Lead Ranking” into the field label.
    • Choose the “Output Data Type for the field in which we want to store the result. In this case, I check the radio button next to the “Number” data type to return the formula result as a number.
    • Move to the next step by clicking the “Next” button.
Salesforce CONTAINS Function
  1. In the advanced formula editor, enter the CONTAINS() function formula. In this case, I use the CONTAINS() function to check the job title against the passed substring and assign a ranking to the lead. The formula is as given below:
IF(
    CONTAINS(Job_Title__c, "Manager"),
    8,
    IF(
        CONTAINS(Job_Title__c, "Engineer") ||
        CONTAINS(Job_Title__c, "Developer"),
        6,
        IF(
            CONTAINS(Job_Title__c, "Analyst"),
            4,
            2
        )
    )
)
  • We use multiple IF statements to check whether the Job_Title__c field contains the specified substring using the CONTAINS function. If it contains, we assign Lead_Rating__c to the lead.
  • Below are the cases that I have used in this example:
    • The lead gets an 8 rating if the word “Manager” appears in the “Job_Title__c” field.
    • The lead is awarded a rating of 6 if the job titles “Engineer” or “Developer” appear in the “Job_Title__c” field.
    • The lead will receive a score of 4 if the word “Analyst” contains in the “Job_Title__c” field.
    • The lead will be given a default score of 2 in all other conditions.
  1. Then, to ensure there is no error in the above-written formula, click on the “Check Syntax” button. You can define the “Description” and “Help Text” as well, and if you want, you can “Handle the empty field”.
    • The above step is a purely optional step if you don’t want to fill up all such things, leave it and directly click on the “Next” button located at the top side of the page.
CONTAINS() function in Salesforce
  1. In this step, establish Field-Level Security. Choose the profiles to which we wish to grant field-level security edit access to this field. If field-level security is not added, the field will be hidden from all profiles.
    • As I want it to be visible to all profiles, I have checked the “Visible” checkbox. After that, click the “Next” button located at the top.
Salesforce Lightning CONTAINS Function
  1. Add the custom field to the Page Layout after setting up the field-level security. Pick the page layout that has this field in it. If we do not select a layout, the field will not appear on any pages.
    • To save the formula field, click the “Save” button.
How to use CONTAINS Function in Salesforce Lightning

You can use the formula field once you have created it. Let’s look at an example:

  • Create a new lead with the Job Title field open in the Lead Tab.
  • Then save it, and move to the Details, here you will see the Lead Ranking field that displays the CONTAINS function result.

As a result, we have learned to use the CONTAINS function in Salesforce Lightning.

Conclusion

In a nutshell, we have learned the Salesforce CONTAINS function. In addition, we have learned about the CONTAINS() function in Salesforce syntax and its application in various real-world business scenarios.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.