Tech blog to share information.

May 18, 2018

Salesforce - Feature Developer asking from Years, Switch Statement

May 18, 2018 Posted by tj cool , , , No comments

Switch Statement - Feature Developer asking for Years


Salesforce Dev community has been asking for switch statement for many years( 4+ years) and finally, Salesforce has introduced "switch" in Summer 18. I don't know why Salesforce has taken so much time to introduce a common statement in programming.

Switch

We all know, Switch statement allow us to change the control flow of program execution via a multiway branch. In a Recent release (Summer-18) Salesforce has finally introduced "switch" statement. Flavor of "switch" that Salesforce has introduced is bit different. What we have seen (at least me) "switch - case", whereas Salesforce has come with "switch-when".
When I used it, I got a feeling of writing "if-else if" 😁 .

This change applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.

Check out the below syntax of a switch-

switch on expression {
    when value1 {  // when block 1
        // code block 1
    } 
    when value2,value3 {  // when block 2
        // code block 2
    }
    when value4 {  // when block 3
        // code block 3
    }
    when else {    // default block, optional
        // code block 4
    }
}

Here when a value can be a single value, multiple values, or sObject types.The switch statement evaluates the expression, executes the code block but if no value matches, then when else code block is executed.

Switch statement expressions support following types.

  • Integer
  • Long
  • sObject
  • String
  • Enum

When values can be of following forms.

  • when literal {} (a when block can have multiple, comma-separated literal clauses)
  • when SObjectType identifier {}
  • when enum_value {}

Some of the Example of Switch

Single Value Example: 

switch on datatype {
   when 'string' {
       string.valueof(variablename);
   }
   when 'decimal' {
       Decimal.valueof(variablename);
   }
   when else {
       System.debug('default');
   }
}

Multiple Values Examples: 

switch on i {
   when 2, 3, 4 {
       System.debug('when block 2 and 3 and 4');
   }
   when 5, 6 {
       System.debug('when block 5 and 6');
   }
   when 7 {
       System.debug('when block 7');
   }
   when else {
       System.debug('default');
   }
}

Working with sObjects : 

switch on sobject {
   when Account a {
       System.debug('account ' + a);
   }
   when Contact c {
       System.debug('contact ' + c);
   }
   when null {
       System.debug('null');
   }
   when else {
       System.debug('default');
   }
}

Working With ENUM-

switch on userType {
   when admin {
       System.debug('admin');
   }
   when guest {
       System.debug('guest');
   }
   when else {
       System.debug('none of the above');
   }
}

References-

Thanks to @Rahul Agrawal for sharing.


Please do comment, like and Subscribe -

0 comments: