In this Salesforce tutorial, we will understand the concept of using the CONTAINS function in Salesforce. We will also learn, real business-related examples with the syntax of the CONTAINS function.
While working in a company that uses Salesforce for customer relationship management, I was stuck in a problem where I need to check whether a text field contains a specific substring or not.
So as a solution, I have found that Salesforce provides the function named “CONTAINS”. If you want to learn how to use the CONTAINS function in the formula field in Salesforce Lightning and Salesforce Classic, let’s deep dive into the complete article and learn.
What is CONTAINS Function in Salesforce
Salesforce CONTAINS() function is one of the Text Functions in Salesforce that is used to carry out the text operations. This CONTAINS function is used to check whether the text field contains the passed substring or not.
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 must be enclosed in the single or double quotation marks.
Furthermore, this CONTAINS function returns the result as a boolean value, True or False, depending upon the string passed and compared with the text field.
Syntax and Usage of CONTAINS Function in Salesforce
Below is the syntax of the CONTAINS function in Salesforce:
- text_field: This text_field parameter specify 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 out within the text field.
Here is an illustration of how to use a formula field’s “CONTAINS” function. Suppose, we want to categorize the cases based on the keywords such as “Prospect”, “Inquiry” or “Interested” to the leads.
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 the example. And now, we will proceed and learn how to use the CONTAINS function in Salesforce Lightning and Classic.
How to use the CONTAINS Function in Salesforce Lightning
Below are the steps to use the CONTAINS function in Salesforce Lightning:
Step 1: Login to the Salesforce account, if you are not in the Lightning, move to the Salesforce Lightning component first. Click on the “Gear” icon on the top right side of the page and from the dropdown, select the “Setup” option to move to the setup of the current app.
Step 2: Next, click on the “Object Manager” option on the top bar of the page next to the Home Tab.
Step 3: The object manager page is opened, search for the object on the “Quick Find Search” box on which you want to use the CONTAINS function. After searching for the object, click on it. In this example, I searched the “Lead” object and click on it.
Step 4: Now you are on the lead object page, move to the “Detail Section” on the left side of the page and click on the option “Field & Relationship”. After this, we will be redirected to the field and relationship page. Now, click on the “New” button at the top of the page.
Step 5: On the click of the “New” button, you will see the list of various field types, choose one from them as you require. As I am going to use the CONTAINS function, for that I need the “Formula” field type, so that I have selected the “Formula” field type.
To move to the next step, click on the “Next” button on the right side of the page.
Step 6: After clicking on the next button, you will redirect to the “Choose Output Type Screen” page. Now, here first enter the information by filling in the “Field Label” and “Field Name”. In this example, I entered “Lead Ranking” as the field label, and then click on the “Field Name” which automatically filled up.
Step 7: Scroll down and choose the data type for the field in which you want to get the result. In this example, I need the result in the Number data type, So, I check the radio button next to the “Number” data type and get the formula return type in the number format. Moreover, I set the “Decimal Place” to 0.
To move to the next step, click on the “Next” button.
Step 8: In the advanced formula editor, enter the CONTAINS() function formula. In this case, I use the CONTAINS() function that check the job title with the passed substring and assigned the 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 that check whether the Job_Title__c field contains the specific passed substring or not 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.
Step 9: 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, as if you want, you can “Handle the empty field”.
Step 10: 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.
Step 11: In this step, you have to set up the “Field-Level Security”. In this, you have to choose the profile to whom you want to grant field-level security edit access to this field. If field-level security is not chosen, 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.
Step 12: Now, you have to add the custom field to the “Page Layouts”. For this, pick the page layout that has this field in it. If you do not choose any page layout, the field will not appear on any pages. Finally, click the “Save” button to save the formula field.
You can use the formula field once you have created it. Let’s look at an example:
- Create a new lead with the field Job Title by opening 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.
With this, we have learned to use the CONTAINS function in Salesforce Lightning. Now, we will move and learn how to use the CONTAINS function in Salesforce Classic.
How to use the CONTAINS Function in Salesforce Classic
Below are the steps to use the CONTAINS Function in Salesforce Classic:
Step 1: Login to Salesforce, and check if you are in the lightning component move to the Salesforce Classic component.
Step 2: To shift to the classic edition, click on the “Profile” dropdown and click on the option“Switch to Salesforce Classic”.
Step 3: You are now in the Salesforce Classic mode, click on the “Setup” option located at the top side of the page next to the Help option.
Step 4: Once the setup page is open, scroll down to the “Build” section, and click on the “Customize” dropdown option, here you will get the list of all the objects. Now search for the object for which want to use CONTAINS function.
Step 5: Here, I search and select the “Accounts” object as I got a task to assign the territory based on the billing state of the account.
Step 6: Click on the accounts object dropdown, here you will get the option of “Fields”. Click on it, as I want to create a new formula field.
Step 7: The orders field page is opened after clicking on the fields option. Now move to the button of the page in the section “Account Custom Fields & Relationship” and click on the “New” button to start the process of creating a new field.
Step 8: In this step, choose the custom field type containing the specific information you want to store. In my case, I check the checkbox next to the “Formula” field type as I want to create the formula that uses the CONTAINS function of Salesforce.
Step 9: To move to the next step, click on the “Next” button.
Step 10: The “Choose Output Type” screen will return after you click the next button. Fill out the “Field Label” and the “Field Name” when you click the field name box. I enter the “Territory” as the Field Label in this instance.
Step 11: Scroll down to the “Formula Return Type” section and choose the return field type for the newly created field. In this case, I check the radio button next to the “Text” output type to get the formula result in the text format.
Step 12: Click on the “Next” button to move to the next proceeding step.
Step 13: Go to the advanced formula editor and enter the CONTAINS function formula there. In this example, I use the CONTAINS function as I have to assign the territory based on the billing state of the account. The formula is as follows:
IF( CONTAINS(BillingState, "California") || CONTAINS(BillingState, "Arizona") || CONTAINS(BillingState, "Nevada"), "West Territory", IF( CONTAINS(BillingState, "New York") || CONTAINS(BillingState, "New Jersey") || CONTAINS(BillingState, "Connecticut"), "East Territory", "Other Territory" ) )
- We use multiple IF statements that check whether the BillingState field contains the specific passed substring or not using the CONTAINS function. If it contains, we set the Territory according to it.
- Below are the cases that I have used in this example:
- The account will be placed in the “West Territory” if the “BillingState” field contains “California”, “Arizona”, or “Nevada”.
- The account will be assigned to the “East Territory” if the “BillingState” field contains the state “New York”, “New Jersey”, or “Connecticut”.
- Where BillingState does not match one of the above states, the account will be assigned to the “Other Territory”.
Step 14: Click on the “Check Syntax” button to validate the formula and ensure no errors.
Step 15: If you want, you can define the “Description”, and “Help Text” as well as “Handle the empty field”.
Step 16: Click the “Next” button to move to the next step.
Step 17: In this step, establish the “Field-Level Security”. Select the profiles to whom you want to grant field-level security editing access. The field will be hidden from all profiles if field-level security is not set.
In this example, I select the “Visible” checkbox because I want it to be visible to all profiles. Then, click on the “Next” button at the top.
Step 18: In this step, add the custom field to the “Page Layout”. For this, choose the page layout that includes this field. The field would not appear on any pages if we choose not to select a layout.
Step 19: Click on the “Save” button to save the formula field.
Once you have established the formula field, you may use it. Let’s see an example.
- Open the Account Tab and create an account with the field Billing State.
- The Territory field, which displays the CONTAINS function output, then appears when you click on the Details Section.
We have learned how to use the Salesforce Classic CONTAINS function.
In a nutshell, we have learned the Salesforce CONTAINS function. In addition, we have learned the CONTAINS() function in Salesforce syntax and various real-world business scenarios.
We have also covered the procedure of using the CONTAINS() function in Salesforce Lightning and Salesforce Classic.
You may like to read:
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.