Apex Switch Statement in Salesforce

Apex Switch Statement in Salesforce

Simplify Your Code with the Apex Switch Statement 
Apex now provides a switch statement that tests whether an expression matches one of several values and branches accordingly.

Apex switch statement expressions can be one of the following types.
Integer
Long
sObject
String
Enum

Sample Code:

for ( Integer i = 1; i <= 3; i++ ) {


switch on i {


when 1 {
System.debug(‘When block 1’);
}


when 2 {
System.debug(‘When block 2’);
}


when 3 {
System.debug(‘When block 3’);
}

when else {
System.debug(‘Default’);
}


}

}

Leave a Reply