CASE Function in Salesforce

In this Salesforce tutorial, we will discuss the CASE function in Salesforce with its use cases and how it works in Salesforce Lightning and Classic editions.

In Salesforce, the CASE function is used to perform conditional logic within a formula. This function allows us to create a series of conditions and specify the values or expressions to be returned based on those conditions.

Table of Contents

What is CASE function in Salesforce

In Salesforce, the CASE function is used to check the series of values of conditional logic defined in the formula. If the expression equals a value, it returns the corresponding result. If the expression is not equal to any values, it returns the else result.

Syntax of CASE function in Salesforce

The Syntax of the CASE function is written in the following way.

CASE(expression,value1,result1,value2,result2. else_result)

In the above syntax, the expression is supposed to be replaced by the field or value you want compared to each specified value.

Replace the value and result with the value that must be equivalent to return the result. If none of the expression is equal to any values the else_result will be returned.

Considerations:

There are some considerations for using the CASE function in Salesforce.

How to use CASE function in Salesforce Lightning

In this example, we will create a custom formula field in the Contact object using the CASE function. This formula will return the engagement level of the contacts. It will return the output as “High”, “Medium” and “Low” according to the activity of the contact record.

To create a custom formula field using the CASE function, follow the steps below.

1. Navigate to the setup page of Salesforce Lightning, and for that click on the Settings icon then in the dropdown select Setup.

2. In the setup window click on the Object Manager tab.

3. In the object manager of lightning, click on the Contact object.

4. In the setup of the Contact object, go to the left sidebar then click on Fields and Relationships. Then in the next window, click on the New button.

Salesforce CASE function execution in lightning

5. In this step, select the field data type as Formula then click on the Next button.

<a href=Custom formula field using CASE function in Salesforce" width="1024" height="366" />

6. In this window, we will define the output of the formula field by entering the following fields.

Salesforce Lightning CASE function use case

7. In this step, enter the below formula in the formula editor.

CASE( LastActivityDate, TODAY() - 30, 'High Engagement', TODAY() - 60, 'Moderate Engagement', TODAY() - 90, 'Low Engagement', 'No Engagement' )

We have applied the following conditionals in the above example.

8. After entering the above formula in the formula editor click on the button Check Syntax to validate the formula then click on the Next button.

CASE function execution in Salesforce

9. In this window, select the user profiles that can have access to this formula field. You can select specific profiles or activate the Visible checkbox to select all profiles and click on the Next button.

Formula field with CASE function in Salesforce Lightning

10. At last activate the page layouts to make this field visible on the page layouts and click on the Save button.

Salesforce Lightning CASE function formula field

As we click on the Save button, the formula field will be saved in the Contact object. Now I will open a contact record to check the output of the formula field.

Track contact status with CASE function in Salesforce

As we can see in the above image the formula field returns the output as No Engagement which means that none of the above conditions match and we got the default value that we defined in the formula.

In this way, we have successfully executed the CASE function in Salesforce Lighting by creating a custom formula field.

How to use Case function in Salesforce Classic

In this example, we will use the CASE function to create a formula field in the Case object. This formula field will return the priority level of the Cases as ‘Urgent’, ‘Normal’, and ‘Low’. The output will be other which is a default value and this default value will be returned when none of the three conditions are true.

To create a formula field using the CASE function in Salesforce Classic, follow the below steps.

1. On the Home page of Salesforce Classic, click on the Setup tab that is on the upper left side of the screen.

2. In the Setup window, go to the Build section in the left sidebar and click on the Customize dropdown. In the dropdown select the object Case -> Fields.

Salesforce Classic CASE function execution

3. In the Fields and Relationships window of the Case object, scroll down to the section Custom Fields and Relationships then click on the New button.

Salesforce Classic custom formula field using CASE function

4. In this window, select the Field Data Type as Formula then click on the Next button.

Salesforce Classic formula field with CASE function

5. In this step, we will enter the following fields to define the output field.

Case function use case in Salesforce Classic

6. In this step, enter the below formula in the formula editor.

CASE(Priority, 'High', 'Urgent', 'Medium', 'Normal', 'Low', 'Low', 'Other' )

We have applied the following condition in the above formula.

7. After entering the formula, click on the button Check Syntax to validate the formula and check errors then click on the Next button.

CASE function execution in Salesforce Classic

8. In this window, either select specific profiles that can access this field or activate the Visible checkbox to select all profiles and click on the Next button.

Use CASE function to create formula field in Salesforce

9. Activate the page layouts to make this field visible on the page layouts. This field will be visible only on the activated page layouts. At last click on the Save button.

<a href=Custom formula field using CASE function in Salesforce Classic" width="1024" height="390" />

Now the formula field is created in the CASE object, and after this, I will open a CASE record to see the output of the formula field.

CASE function formula field output in Salesforce

As we can see in the above image the formula field returns the output as Normal which means the priority was set to Medium.

In this way, we have successfully executed the CASE function in Salesforce Classic by creating a custom formula field.

Conclusion

In this Salesforce tutorial, we have learned about the CASE function where we created the custom formula fields in Salesforce Lightning and Salesforce Classic with the help of the CASE function.

Along with this we also learned about the syntax of the Case function and later we discussed the considerations that we should be aware of while using the CASE function in Salesforce.

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.