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.

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.

  • While using the CASE function, ensure that value1, value2… expressions are of the same data type.
  • The result1, result2… expressions in the CASE function should be of the same data type.
  • It is mandatory to define the else_result in the CASE function.
  • CASE functions can’t contain functions that return true or false.
  • CASE functions return an error whenever any of the expressions return an error, regardless of which one should be returned. 
  • Whenever the CASE function is left blank, it will return else_result.
  • The CASE function is used to assess whether a picklist value is equal to a particular value.

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.

Custom formula field using CASE function in Salesforce

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

  • Enter the name for the output field in the Field Label. In this example, I have entered Field Label as Contact Status.
  • The Field Name will be auto-filled according to the entered Field Label as we click on it.
  • In the Formula Return Type, select the option Text then click on the Next button.
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.

  • In the above formula, the CASE function categorizes the contacts based on the LastActivityDate.
  • TODAY() – 30, ‘High Engagement’: If the Last Activity date is within the last 30 days from today, the formula returns ‘High Engagement’.
  • TODAY() – 60, ‘Moderate Engagement’: If the Last Activity date is within the last 60 days from today, the formula returns ‘Moderate Engagement’.
  • TODAY() – 90, ‘Low Engagement’: If the Last Activity date is within the last 90 days from today, the formula returns ‘Low Engagement’.
  • If none of the above conditions are applied, the formula returns ‘No Engagement’. This serves as the default value.

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.

  • Enter the output field name in the Field Label. In this example, I have entered Field Label as Case Priority.
  • The Field Name will be auto-filled according to the entered Field Label as we click on it.
  • In the Formula Return Type, select the option Text then click on the Next button.
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.

  • If the Priority is set to High the output will be Urgent.
  • If the Priority is set to Medium the output will be Normal.
  • If the Priority is set to Low the output will be Low.
  • If none of the above conditions are not true the output will be Other.

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.

Custom formula field using CASE function in Salesforce Classic

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: