I am trying to use a switch statement with constants but it doesn't appear that the switch statement can see the constants.

At the top of the class I have: private final String A_B_C = 'a b c';

Then later in the class I am attempting:

switch on OpportunityTriggerHelper.recordTypes.get(opp.RecordTypeId).getDeveloperName() {
        when A_B_C {
            // do something

What I get is variable doesn't exist A_B_C.

I know for a fact that that variable is visible because there's no problem using it in an if statement.

Is this a bug or am I doing something incorrectly?

1 Answer 1


The when clause can accept the following three types of values

A) when literal {} (a when block can have multiple, comma-separated literal clauses)

B) when SObjectType identifier {}

C) When enum_value {}

Anything apat from this will give a compiletime error.

In your case,

private final String A_B_C = 'a b c'; is a variable and not a literal and hence it gives you an error.

Src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .