美文网首页Salesforce 开发笔记
2020-12-03 Set Approval Process

2020-12-03 Set Approval Process

作者: 古月的小七 | 来源:发表于2020-12-03 10:44 被阅读0次

    我们会遇到这样的需求:当对象处于某种状态时,希望在该状态下该条数据不能被更改,即Lock的状态,具体的实现方法如下:

    • Setting
      在Setup中 Search “Process Automation Setting”,然后 Enable record locking and unlocking in Apex


      image.png
    • Apex class , 下面就是 Apex的具体实现
    Lock Logic:
    // Query the accounts to lock
    Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Acme%'];
    // Lock the accounts
    Approval.LockResult[] lrList = Approval.lock(accts, false);
    
    // Iterate through each returned result
    for(Approval.LockResult lr : lrList) {
        if (lr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully locked account with ID: ' + lr.getId());
        }
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : lr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Account fields that affected this error: ' + err.getFields());
            }
        }
    }
    
    下面是Unlock Logic
    //Get records to unlock
    List<case> caseList = [SELECT Id From Case LIMIT 10];
    //Check locked records
    List<case> caseLockList = new List<Case>();
    for(Case c :caseList){
        if(Approval.isLocked(c.id)){
            caseLockList.add(c);
        }
    }
    //Unlock record
    if(!caseLockList.isEmpty()){
        //Unlock records
        List<Approval.UnlockResult> ulrList = Approval.unlock(caseLockList, false);
         
        // Iterate through each returned result
        for(Approval.UnlockResult  ulr : ulrList) {
            if (ulr.isSuccess()) {
                //Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully locked account with ID: ' + ulr.getId());
            }
            else {
                //Operation failed, so get all errors                
                for(Database.Error err : ulr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Case fields that affected this error: ' + err.getFields());
                }
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:2020-12-03 Set Approval Process

        本文链接:https://www.haomeiwen.com/subject/mjqmwktx.html