The syntax is:
switch on expression { when value1 { // when block 1 // code block 1 } when value2 { // when block 2 // code block 2 } when value3 { // when block 3 // code block 3 } when else { // default block, optional // code block 4 } }
The when value can be a single value, multiple values, or sObject types. For example:
when value1 { }
when value2, value3 { }
when TypeName VariableName { }
The switch statement evaluates the expression and executes the code block for the matching when value. If no value matches, the when else code block is executed. If there isn’t a when else block, no action is taken.
Each when block has a value that the expression is matched against. These values can take one of the following forms.
The value null is a legal value for all types.
Each when value must be unique. For example, you can use the literal x only in one when block clause. A when block is matched one time at most.
If no when values match the expression, the when else block is executed.
If you include a when else block, it must be the last block in the switch statement.
You can use literal when values for switching on Integer, Long, and String types. String clauses are case-sensitive. For example, “orange” is a different value than “ORANGE.”
The following example uses integer literals for when values.
switch on i { when 2 { System.debug('when block 2'); } when -3 { System.debug('when block -3'); } when else { System.debug('default'); } }
Because all types in Apex are nullable, a when value can be null.
switch on i { when 2 { System.debug('when block 2'); } when null { System.debug('bad integer'); } when else { System.debug('default ' + i); } }
The Apex switch statement doesn’t fall-through, but a when clause can include multiple literal values to match against. You can also nest Apex switch statements to provide multiple execution paths within a when clause.
switch on i { when 2, 3, 4 { System.debug('when block 2 and 3 and 4'); } when 5, 6 { System.debug('when block 5 and 6'); } when 7 { System.debug('when block 7'); } when else { System.debug('default'); } }
Instead of switching on a variable expression, the following example switches on the result of a method call.
switch on someInteger(i) { when 2 { System.debug('when block 2'); } when 3 { System.debug('when block 3'); } when else { System.debug('default'); } }
Switching on an sObject value allows you to implicitly perform instanceof checks and casting. For example, consider the following code that uses if-else statements.
if (sobject instanceof Account) { Account a = (Account) sobject; System.debug('account ' + a); } else if (sobject instanceof Contact) { Contact c = (Contact) sobject; System.debug('contact ' + c); } else { System.debug('default'); }
You can replace and simplify this code with the following switch statement.
switch on sobject { when Account a { System.debug('account ' + a); } when Contact c { System.debug('contact ' + c); } when null { System.debug('null'); } when else { System.debug('default'); } }
A switch statement that uses enum when values doesn’t require a when else block, but it is recommended. You can use multiple enum values per when block clause.
switch on season { when WINTER { System.debug('boots'); } when SPRING, SUMMER { System.debug('sandals'); } when else { System.debug('none of the above'); } }