Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 776
David W Grigsby Avatar asked Mar 02 '14 03:03

David W Grigsby


People also ask

Does Apex support switch statement in Salesforce?

Apex provides a switch statement that tests whether an expression matches one of several values and branches accordingly.

What types of collection does Apex supports?

There are three different types of collections in apex: List, Set and Map. Collections are composite data types which allow the developer to aggregate, or collect, multiple other types into a single variable.

Is switch statement is used to execute all the case?

The switch statement can include any number of case instances. However, no two constant-expression values within the same switch statement can have the same value. Execution of the switch statement body begins at the first statement in or after the matching labeled-statement .

Is Apex compiled or interpreted?

Apex is interpreted, executed, and controlled entirely by the Lightning Platform. Like the rest of the Lightning Platform, Apex runs in a multitenant environment. So, the Apex runtime engine is designed to guard closely against runaway code, preventing it from monopolizing shared resources.


2 Answers

switch statement support is coming to Apex in the Summer '18 release.

From the TrailheadX 2018 session Switching It Up with Apex:

enter image description here

Initially it will support Enums, String, Integer, and Long

like image 180
Daniel Ballinger Avatar answered Oct 07 '22 19:10

Daniel Ballinger


Meanwhile SFDC is providing native engine, as work-around you can use small utility 'framework' as switch-case object-oriented 'statement':

Switch-Case Utility

Example usage:

public with sharing class SwitchCaseExample {

    public String result {get; set;}

    public static final String MSG_FROM_ACTION_1 = 'invoke action 1';
    public static final String MSG_FROM_ACTION_2 = 'invoke action 2';
    public static final String MSG_FROM_ACTION_3 = 'invoke action 3';
    public static final String MSG_FROM_ACTION_4 = 'invoke action 4';

    public void testSwitchCase(String value) {

        SwitchCaseHelper sch = new SwitchCaseHelper();  

        sch.switch(value)
            .case('value1', new Action1(this), SwitchCaseHelper.PUT_BREAK)
            .case('value2', new Action2(this), SwitchCaseHelper.PUT_CONTINUE)
            .case('value3', new Action3(this), SwitchCaseHelper.PUT_BREAK)
            .default(new Action4(this));
    }


    private class Action1 implements ActionContainer {

        private SwitchCaseExample outerCtx;

        public Action1(SwitchCaseExample outerCtx) {

            this.outerCtx = outerCtx;
        }

        public String doAction() {

            outerCtx.result = MSG_FROM_ACTION_1;
            return null; 
        }
    }

    private class Action2 implements ActionContainer {

        private SwitchCaseExample outerCtx;

        public Action2(SwitchCaseExample outerCtx) {

            this.outerCtx = outerCtx;
        }

        public String doAction() {

            outerCtx.result = MSG_FROM_ACTION_2;
            return null; 
        }
    }

    private class Action3 implements ActionContainer {

        private SwitchCaseExample outerCtx;

        public Action3(SwitchCaseExample outerCtx) {

            this.outerCtx = outerCtx;
        }

        public String doAction() {

            outerCtx.result = MSG_FROM_ACTION_3;
            return null; 
        }
    }

    private class Action4 implements ActionContainer {

        private SwitchCaseExample outerCtx;

        public Action4(SwitchCaseExample outerCtx) {

            this.outerCtx = outerCtx;
        }

        public String doAction() {

            outerCtx.result = MSG_FROM_ACTION_4;
            return null; 
        }
    }

}

Interface:

public interface ActionContainer {

    String doAction();

}

And switch-case logic implementation

public with sharing class SwitchCaseHelper {

    public static final Boolean PUT_BREAK = true;
    public static final Boolean PUT_CONTINUE = false;

    public class SwitchCaseException extends Exception {}

    public static final String EXCEPTION_MESSAGE = 'Switch-Case construction must have one (and only one) "switch" statement';

    @TestVisible
    private Object switchOperand;

    @TestVisible
    private Boolean isCaseAfterBreakStatement;

    @TestVisible
    private Boolean isPreviousSwitch;

    public SwitchCaseHelper() {

        isCaseAfterBreakStatement = false;
    }

    public SwitchCaseHelper switch(Object switchOperand) {

        if (isPreviousSwitch != null) {
            throw new SwitchCaseException(EXCEPTION_MESSAGE);
        }
        isPreviousSwitch = true;
        this.switchOperand = switchOperand;
        return this;
    }

    public SwitchCaseHelper case(Object caseOperand, ActionContainer container, Boolean hasBreak) {

        if (isPreviousSwitch == null) {
            throw new SwitchCaseException(EXCEPTION_MESSAGE);
        }

        if (isPreviousSwitch) {
            isPreviousSwitch = false;
        }

        if (isCaseAfterBreakStatement) {
            return this;
        }

        if (switchOperand.equals(caseOperand)) {
            container.doAction();
            isCaseAfterBreakStatement = hasBreak;
        }

        return this;
    }

    public SwitchCaseHelper default(ActionContainer container) {

        if (isPreviousSwitch == null) {
            throw new SwitchCaseException(EXCEPTION_MESSAGE);
        }

        if (!isCaseAfterBreakStatement) {
            container.doAction();
        }
        return this;
    }
}
like image 38
Vladimir Avatar answered Oct 07 '22 17:10

Vladimir