Salesforce 开发笔记

2020-12-03 Set Approval Process

2020-12-03  本文已影响0人  古月的小七

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

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());
            }
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读