function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumpritsumprit 

Can we write a SWITCH statement in APEX ?

Hi there,

Can we use a SWITCH control statement in the APEX class? Is it possible and supported by APEX?

thanks
ssikorassikora
No
sumpritsumprit
I have a following class:-

============================================

public class UpdateProduct{
public static void ProductName(Account[] accs){
for (Account a: accs){
if (a.ProductName__c == 'KarPower'){
a.KARPOWER__c = True;
}
}
}
}

===========================================

A TRIGGER is created on Account object to update the custom fields, which is more
than 20 fields. So each time a user selects a product , the related name /custom field
automatically gets CHECKED (a check mark is placed here).

What is the best approach to do that?

Should I create If statements for all of these 20 custom fields?

thanks again

sumpritsumprit
I beleive this is how it will look like with 20 fields or more in an IF statement.

public class UpdateProduct{
public static void ProductName(Account[] accs){
for (Account a: accs){
if (a.ProductName__c == 'KarPower'){
a.KARPOWER__c = True; }
if (a.ProductName__c == 'Books'){
a.Books__c = True; }
if (a.ProductName__c == 'PriceLink'){
a.Pricelink__c = True; }
if (a.ProductName__c == 'Product KarPower'){
a.Pocket_KarPower__c = True; }


}
}

And then TRIGGER is created on Account object to call this class .

=========================================================
trigger Prody1 on Account (before insert, before update) {

Account[] accs = Trigger.new;
UpdateProduct.ProductName(accs);



}
}
=========================================================


cool isnt it!

rock krishrock krish
apex does not support SWITCH CASE ...so we can use replaced by
if(){
}  
else if () {
}
.
.
.
else {
}
Mark Tyler CrawfordMark Tyler Crawford
This is still a limitation, SWITCH CASE is not supported. However, it appears that with the roll out of the new compiler, this will become available (timeframe still not known). See the following article for more details: https://success.salesforce.com/ideaView?id=08730000000BrSIAA0 
Purushotham YellankiPurushotham Yellanki
Hi Sumprit,

FYI - With Summer '18 release, Switch statements are supported in Apex!

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


Thank you 
Ketan Benegal 7Ketan Benegal 7
Following is the syntax of switch statement (available from Summer '18)

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


expression can be of following types:
Integer
Long
sObject
String
Enum

If no when values match the expression, the when else block is executed.

Thanks,
KB