Newer Version Available
Switch Statements
The syntax is:
1switch on expression {
2 when value1 { // when block 1
3 // code block 1
4 }
5 when value2 { // when block 2
6 // code block 2
7 }
8 when value3 { // when block 3
9 // code block 3
10 }
11 when else { // default block, optional
12 // code block 4
13 }
14}The when value can be a single value, multiple values, or sObject types. For example:
1when value1 {
2}1when value2, value3 {
2}1when TypeName VariableName {
2}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.
- Integer
- Long
- sObject
- String
- Enum
When Blocks
Each when block has a value that the expression is matched against. These values can take one of the following forms.
- when literal {} (a when block can have multiple, comma-separated literal clauses)
- when SObjectType identifier {}
- when enum_value {}
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.
When Else Block
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.
Examples with Literals
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.
1switch on i {
2 when 2 {
3 System.debug('when block 2');
4 }
5 when -3 {
6 System.debug('when block -3');
7 }
8 when else {
9 System.debug('default');
10 }
11}Because all types in Apex are nullable, a when value can be null.
1switch on i {
2 when 2 {
3 System.debug('when block 2');
4 }
5 when null {
6 System.debug('bad integer');
7 }
8 when else {
9 System.debug('default ' + i);
10 }
11}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.
1switch on i {
2 when 2, 3, 4 {
3 System.debug('when block 2 and 3 and 4');
4 }
5 when 5, 6 {
6 System.debug('when block 5 and 6');
7 }
8 when 7 {
9 System.debug('when block 7');
10 }
11 when else {
12 System.debug('default');
13 }
14}Instead of switching on a variable expression, the following example switches on the result of a method call.
1switch on someInteger(i) {
2 when 2 {
3 System.debug('when block 2');
4 }
5 when 3 {
6 System.debug('when block 3');
7 }
8 when else {
9 System.debug('default');
10 }
11}Example with sObjects
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.
1if (sobject instanceof Account) {
2 Account a = (Account) sobject;
3 System.debug('account ' + a);
4} else if (sobject instanceof Contact) {
5 Contact c = (Contact) sobject;
6 System.debug('contact ' + c);
7} else {
8 System.debug('default');
9}You can replace and simplify this code with the following switch statement.
1switch on sobject {
2 when Account a {
3 System.debug('account ' + a);
4 }
5 when Contact c {
6 System.debug('contact ' + c);
7 }
8 when null {
9 System.debug('null');
10 }
11 when else {
12 System.debug('default');
13 }
14}Example with Enums
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.
1switch on season {
2 when WINTER {
3 System.debug('boots');
4 }
5 when SPRING, SUMMER {
6 System.debug('sandals');
7 }
8 when else {
9 System.debug('none of the above');
10 }
11}