10 Years Later, Salesforce Introduced the Apex Switch Statement in Summer 2018 Release

Poster 2 basic

This will be a glad news for all salesforce developers who were looking for an apex switch statement for decades to simplify most of the lengthy apex statements. Exactly 10years back there has been an Idea posted on the Idea exchange for this.

IdeaExchange Link – https://success.salesforce.com/ideaView?id=08730000000BrSIAA0

Apex switch verifies that the expression provided matches with the any of the values of its own branches. The syntax is.

switch on expression {
    when value1 {		// when block 1
        // code block 1
    when value2, Value3 {		// when block 2
        // code block 2
    when Typename variableName{		// when block 3
        // code block 3
    when else {		  // when else block, optional
        // code block 4

Compared to switch in other high-level languages, It supports

  • Single & multiple value matches in the cases
  • object reference match
  • the result of a method call can be considered as an expression instead of variable expression
  • directly support with enums

Switch expression supports the following data types

  • Integer
  • Long
  • sObject
  • String
  • Enum

Single, Multiple and Null Value example

Switch statements evaluate the expression and execute the first matching when block otherwise else block if any.  Apex switch doesn’t encounter with fall through issue since control exits from the switch when the matching block execution is over. 

String color = 'Y';
//Expression should be any of the allowable type value
switch on color {
when 'V' {
when 'I' {
//Multiple values given as comma seperated
when 'B','G' {
System.debug('Blue or Green');
when 'Y','O','R' {
System.debug('Yellow or Orange or Red');
when null {
System.debug('Bad Value');
when else {
System.debug('Not in spectrum');

view raw


hosted with ❤ by GitHub

Example with SObjects

Explicit typecasting is the key to extracting the content from a SObject and instanceof method can help to identify the SObject value type. See the below example.

sobject obj = new Contact();
if(obj instanceof Account) {     
   Account acc = (Account)obj;     
   System.debug('account ' + acc);
} else if(obj instanceof Contact) {
     Contact con = (Contact)obj; 
     System.debug('Contact ' + con);    
} else if (obj == null) {
} else {

The Apex Switch implicitly performs the casting and type checking. See below example.

sobject obj = null;
switch on obj {
//checks whether the obj is an instance of Account.
//if yes, obj is casted to acc with relevant values
when Account acc {
System.debug('account ' + acc);
//checks whether the obj is an instance of Contact.
//if yes, obj is casted to con with relevant values
when Contact con {
System.debug('contact ' + con);
//checks obj == null
when null {
// default block
when else {

Note : You can use only one sObject type per when block.

The switch statement can also support the enum type with multiple values in the when block.


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

Summer 18 release notes : https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_apex_switch_statement.htm

Poster 2 basic

Don’t forget to share the post if you like it & bookmark the blog for future references. If you have any comments or doubts on this post, Please comment on the box.

Leave a Reply

%d bloggers like this: