Salesforce开发之 如何实现DML操作时,当前用户跳过验证规则(Validation Rule)

2022-08-08 17:52:22 浏览数 (3)

代码语言:javascript复制
    public static void ignoreValidationRules(List<Sobject> dmlList, String dmlType) {
        //跳过验证规则
        GlobalSetting__c gs;
        GlobalSetting__c oldGS;
        List<GlobalSetting__c> gsList = [SELECT Id,
                                                SetupOwnerId,
                                                IgnoreValidation__c
                                         FROM GlobalSetting__c
                                         WHERE SetupOwnerId = :UserInfo.getUserId()];
        // 设置当前用户忽略验证规则
        if (gsList.size() > 0) {
            gs = gsList[0];
        } else {
            gs = new GlobalSetting__c();
            gs.SetupOwnerId = UserInfo.getUserId();
        }
        oldGS = gs.clone();
        gs.IgnoreValidation__c = true;
        UPSERT gs;

        oldGS.Id = gs.Id;

        if (dmlType == 'INSERT') {
            insert dmlList;
        }
        if (dmlType == 'UPDATE') {
            update dmlList;
        }
        if (dmlType == 'DELETE') {
            delete dmlList;
        }

        UPDATE oldGS;
    }

0 人点赞