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. 

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) {
    System.debug('null');
} else {
    System.debug('default');
}

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

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.

References

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s