Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

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

Asked by Dipesh Bhardwaj Jun 7, 2023 3.1K views 2 answers
Share

About this question

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.

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jun 3, 2024

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.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.