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.