TIP OF THE WEEK: APEX Switch Statements

APEX Switch Statements was introduced in the Salesforce Summer 18 release.

 

What are APEX Switch Statements? Switch Statements are a programming control structure. They can be a substitute when using nested if-else control structures. Until the Summer 18 Release, Salesforce APEX code didn’t have this ability. Did you know that This Salesforce idea has been circling on the Idea Exchange since 2008? That’s why we’re very excited about this feature, as it will provide some much-needed help with the code control structure.

 

 

 

Example of Case Statement in Java. 

 

 

Example code for APEX Switch statements:

 

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
}
}

 

All new Switch Statements will enable developers the ability to write APEX code without having to write nested if-else blocks, eventually resulting in more readable and maintainable code.

 

You can read more here:

Salesforce Documentation on Switch statements

Release notes for Switch statements