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
jgrenfelljgrenfell 

Unable to do Switch (case) statement

I'm trying to do a switch statement in Apex, I'm assuming the syntax will be like java, but I can't seem to get it to compile; I get an "unexpected token: {" error message. Am I missing something obvious?  I have many more cases to add and I'm hoping I don't have to do if statements for each one.

Code:
switch (ce_days) {
 case 180:
  milestone = 180;
}//end switch

 

ssikorassikora
There is no support for switch() at this time as per the Apex language reference.
WilmerWilmer

You could try an if...else if statement instead.

if (place = 1) {
   medal_color = 'gold';
} else if (place = 2) {
   medal_color = 'silver';
} else if (place = 3) {
   medal_color = 'bronze';
} else {
   medal_color = null;
}

jgrenfelljgrenfell
That's what I ended up doing, it's just a scenario that comes up a lot and a Case statement is a lot cleaner way of doing it.  I know now that "case" is a term reserved for future releases, so I'll just have to be patient.
Sakshi Nagpal 30Sakshi Nagpal 30
Switch case is supported in salesforce from Winter 18
Refer this ideahttps://success.salesforce.com/ideaview?id=08730000000BrSIAA0