Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

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

Asked by Ajit Yadav Mar 28, 2021 774 views 2 answers
Share

About this question

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 intent clarity.

Your answer

2 Answers

Pallavi Patil Latest answer

Answered on Jun 8, 2021

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.