How to Use Switch Statement in Salesforce Apex

In the recent Salesforce Apex development, I assigned priority levels to customer support cases based on their issue type, such as “technical,” “billing,” etc. I had to assign a priority to each case, like “high,” “medium,” or “low,” based on the issue type.

I could have used if-else statements, but management handled multiple issues throughout the year. Manually writing if-else statements for each case would have been challenging to maintain due to the increased number of cases.

As a solution, I used the Switch case statement to match different case types and assign a specific priority value. In this Salesforce tutorial, I will explain how to use switch statements in Salesforce Apex.

What is the Switch Statement in Salesforce Apex?

In Salesforce Apex development, Switch statements were not introduced until their release in Winter ’18. Switch statements are used as case statements in Salesforce Apex or any other development language.

They are similar to the if, else, and elif conditional statements. Even though they are part of the Apex language, their syntax is slightly different from that of the standard switch case.

Syntax of Switch Statement

In Salesforce Apex, the switch statement runs corresponding code blocks after testing an expression against a set of values. Below is the syntax for the Switch statement.

switch on expression {

    when value1 {

        // code for value1

    }

    when value2 {

        // code for value2

    }

    …

    when else {

        // default code block

    }

}

Switch Statement in Salesforce Apex

In the steps below, I will use the above-explained scenario as a case for the Switch statement in the Salesforce Apex code.

  1. Navigate to the Salesforce developer console and create a new class by selecting File>New>Class.
  2. Enter the name of the public class and click the OK button. Enter the code below in the developer console and save it.
public class SwitchCaseAssignment {
    public static String assignCasePriority(String IssueType) {
        String Priority;
        
        switch on IssueType {
            when 'Technical' {
                Priority = 'High';
            }
            when 'Billing' {
                Priority = 'Medium';
            }
            when 'Account' {
                Priority = 'Low';
            }
            when 'Other' {
                Priority = 'Low';
            }
            when else {
                priority = 'Unknown';
            }
        }     
        return priority;
    }
}
  1. If the code has no errors (problems), execute it with the shortcut keys “Ctrl+E.
    • In the anonymous execution window, enter the code below to test the switch statement. After entering the code, select the checkbox Open in Debug and click the Execute button.
String[] testInputs = new String[] {'Technical', 'Billing', 'Account', 'Other', 'UnknownType'};

for (String input : testInputs) {
    String result = SwitchCaseAssignment.assignCasePriority(input);
    System.debug('Priority for IssueType "' + input + '" is: ' + result);
}
  1. In the testinputs, we have entered all the values of the issue type for the case. Then, SwitchCaseAssignment.assignCasePriority(input) will assign the priority level of the cases according to the input values.
  1. In the Console log window, select the checkbox to debug only. You will then see the priority level for all the input string values we entered in the anonymous window.
Use Switch Statement in Salesforce apex

This way, we can use the Switch statements in Salesforce Apex using the above syntax. It helps handle large datasets, where you can avoid using if, else, and elif statements and replace them with Apex Switch.

Consideration for Switch Statement in Salesforce Apex

Below are the considerations for using Switch statements in Salesforce Apex that we should be aware of.

  • The Switch statement blocks start from when, and the values for the when can be single, multiple, or sObject types.
  • We can define multiple when conditions in an apex Switch statement.
  • The else conditional statement is in the last block of the Switch. The else statement is executed when no values for the when conditions match.
  • Apex switch statements can be made using one of the following types:
  • There are no break statements in the switch like we used to have in other programming languages.
  • We don’t have a case statement in the Apex programming language. The reason behind this is that a Case is an sObject in Apex. That is why Salesforce is having issues with the keyword.
  • The null value is considered legal for all data types in a switch statement.

Conclusion

In the Salesforce tutorial, we learned about Switch statements, and with a real-time example, we learned how to use a switch statement in Salesforce Apex.

After that, we also discussed the considerations for the Switch function that we should be aware of while using the Switch statement in Salesforce Apex.

By now, you will be able to execute the switch statements in your Salesforce Apex and enhance your Apex code structure.

You may also 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.