Salesforce中的APEX是否支持SWITCH和CASE语句?

时间:2014-03-02 03:16:27

标签: salesforce apex

而不是IF / THEN / ELSEIF / ELSE的基本结构

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

很高兴

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

这会增加可读性,并且由于意图清晰而使调试更容易。

6 个答案:

答案 0 :(得分:13)

2018年 - 是的!!!! - 如果最后是 - 好消息 - Switch Now supported 跳至视频{3}}

的确切时间

2014年 - 此时此刻悲惨。自2009年以来,我一直在等待此功能,并且通过以下社区的链接获得了高度要求。

YouTube of Session at golden moment

答案 1 :(得分:9)

    monthMap = new Map<Integer,String>();
    monthMap.put(1,'JAN');
    monthMap.put(2,'FEB');
    monthMap.put(3,'MAR');
    monthMap.put(4,'APR');
    monthMap.put(5,'MAY');
    monthMap.put(6,'JUN');
    monthMap.put(7,'JUL');
    monthMap.put(8,'AUG');
    monthMap.put(9,'SEP');
    monthMap.put(10,'OCT');
    monthMap.put(11,'NOV');
    monthMap.put(12,'DEC');

然后根据您的整数月值进行获取。

不需要写一个大的if-else。

答案 2 :(得分:4)

switch声明支持即将于18年夏季发布给Apex。

来自TrailheadX 2018会话https://support.hdfgroup.org/HDF5/examples/api-c.html

Switching It Up with Apex

最初它将支持Enums,String,Integer和Long

答案 3 :(得分:3)

我已将其评论添加到其他答案中。

虽然这个问题并没有真正回答这个问题,但我仍然认为把它放在这里是一个好主意。我讨厌看到这样的&#34;自制日期库&#34; ...

DateTime someDate = System.now();
System.debug(someDate.format('MMM'));  // Jan, Feb etc.
System.debug(someDate.format('MMMM')); // January, February etc.

即使当前用户的语言偏好不同,它也总是用英语。格式化字符串将传递给内部Java方法,因此只需快速查看http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

即可

答案 4 :(得分:2)

同时SFDC提供本机引擎,因为你可以使用小实用程序'框架'作为switch-case面向对象的'语句':

Switch-Case Utility

使用示例:

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

}

接口:

public interface ActionContainer {

    String doAction();

}

和案例逻辑实现

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

答案 5 :(得分:0)

  

Apex现在支持切换语句(来自Summer&#39; 18发布):

在此完整答案:https://success.salesforce.com/answers?id=90630000000wkANAAY

通过Release notes

其中:此更改适用于Enterprise,Performance,Unlimited和Developer版本中的Lightning Experience和Salesforce Classic。

如何: 语法是:

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 {       // when else block, optional
        // code block 4
    }
}

PS:在此处添加以供将来参考。