Is the SWITCH and CASE statement supported in APEX in Salesforce?

David W Grigsby picture David W Grigsby · Mar 2, 2014 · Viewed 27.7k times · Source

Instead of this basic structure with IF / THEN / ELSEIF / ELSE

int month = 8;
String monthString;
if (month == 1) {
    monthString = "January";
} else if (month == 2) {
    monthString = "February";
}
...  // and so on

it would be nice to have

    int month = 8;
    String monthString;
    switch (month) {
        case 1:  monthString = "January";
                 break;
        case 2:  monthString = "February";
                 break;

        ..... // and so on

        case 12: monthString = "December";
                 break;
        default: monthString = "Invalid month";
                 break;
    }

This would increase readability and make it easier to debug due to clarity of intent.

Answer

David W Grigsby picture David W Grigsby · Mar 2, 2014

2018 - Yeah!!!! - If Finally is - Great News - Switch Now supported Skip to the exact time in Video at YouTube of Session at golden moment

2014 - Not at this time sadly. I have been waiting since 2009 for this feature and it is a highly requested one by the link from the community below.

Add "Switch" or "Case" Statement to Apex