How to use variables in the value of an apex switch statement?

847    Asked by dipesh_9001 in Salesforce , Asked on Jun 7, 2023

When using a variable in value of switch statement, I get an error: Error: Field must be an enum reference

Apex code:

static final String VALUE_ORANGES = 'Oranges'; String expr = 'Oranges'; switch on expr { when VALUE_ORANGES { System.debug('Oranges are $0.59 a pound.'); } when else { System.debug('Sorry, we are out of ' + expr +'.'); } }

Why doesn't this work? The only option is to use enum? To compare with other languages, similar code works fine in JavaScript.

Answered by Elizabeth Jordan

The apex switch statement works as it does in many (but not all) other languages, which is to say that, under the hood, the compiler optimises the layout of the switch statement's bytecode so that it runs in fixed time (O(1)). This is one of the reasons that makes the switch statement more attractive than the if-then-else chain, which has to potentially evaluate every single value in the chain.

The tradeoff for this performance boost is that you cannot use variables, only literal values. If variables were allowed, it would change the switch's execution time to linear execution time O(n), because the compiler would no longer be able to optimise the bytecode, which in turn would be mean that the more when branches you have, the longer it would take to evaluate the switch statement.


Your Answer

Answer (1)

In Apex, you can use variables in the value of a switch statement by defining the variable before the switch statement and using it directly in the switch expression. Here's a simple example to illustrate how to use variables in the value of an Apex switch statement:

public class SwitchExample {
    public static void runSwitchExample() {
        String status = 'InProgress';
        switch on status {
            when 'New' {
                System.debug('The status is New.');
            }
            when 'InProgress' {
                System.debug('The status is InProgress.');
            }
            when 'Completed' {
                System.debug('The status is Completed.');
            }
            when else {
                System.debug('The status is Unknown.');
            }
        }
    }
}

In this example:

We define a variable status and set it to 'InProgress'.

The switch statement is used to check the value of the status variable.

Depending on the value of status, the appropriate when block is executed.

If you want to use a variable within the when condition, you can use a constant or another variable. Here’s an example using a constant:

public class SwitchExample {
    public static final String STATUS_NEW = 'New';
    public static final String STATUS_IN_PROGRESS = 'InProgress';
    public static final String STATUS_COMPLETED = 'Completed';
    public static void runSwitchExample() {
        String status = 'InProgress';
        switch on status {
            when STATUS_NEW {
                System.debug('The status is New.');
            }
            when STATUS_IN_PROGRESS {
                System.debug('The status is InProgress.');
            }
            when STATUS_COMPLETED {
                System.debug('The status is Completed.');
            }
            when else {
                System.debug('The status is Unknown.');
            }
        }
    }
}

In this example, the constants STATUS_NEW, STATUS_IN_PROGRESS, and STATUS_COMPLETED are used in the when conditions, making the code more readable and maintainable.

Remember, the switch statement in Apex can only be used with specific types of variables: Integer, Long, String, Enum, and SObjectType. Make sure your variable is of one of these types when using it in a switch statement.

1 Month

Interviews

Parent Categories