Namespace
Usage
Example
The following example shows how to obtain and iterate through the returned Approval.UnlockResult objects. It locks some queried accounts using Approval.unlock with a false second parameter to allow partial processing of records on failure. Next, it iterates through the results to determine whether the operation was successful for each record. It writes the ID of every record that was processed successfully to the debug log, or writes error messages and failed fields of the failed records.
// Query the accounts to unlock Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Acme%' WITH USER_MODE]; for(Account acct:accts) { // Create an approval request for the account Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); req1.setComments('Submitting request for approval.'); req1.setObjectId(acct.id); // Submit the record to specific process and skip the criteria evaluation req1.setProcessDefinitionNameOrId('PTO_Request_Process'); req1.setSkipEntryCriteria(true); // Submit the approval request for the account Approval.ProcessResult result = Approval.process(req1); // Verify the result Assert.isTrue(result.isSuccess()); } // Unlock the accounts Approval.UnlockResult[] urList = Approval.unlock(accts, false); // Iterate through each returned result for(Approval.UnlockResult ur : urList) { if (ur.isSuccess()) { // Operation was successful, so get the ID of the record that was processed System.debug('Successfully unlocked account with ID: ' + ur.getId()); } else { // Operation failed, so get all errors for(Database.Error err : ur.getErrors()) { System.debug('The following error has occurred.'); System.debug(err.getStatusCode() + ': ' + err.getMessage()); System.debug('Account fields that affected this error: ' + err.getFields()); } } }
UnlockResult Methods
The following are methods for UnlockResult.
getErrors()
Signature
public List<Database.Error> getErrors()
Return Value
Type: List<Database.Error>
getId()
Signature
public Id getId()
Return Value
Type: Id
Usage
If the field contains a value, the object was unlocked. If the field is empty, the operation was not successfult.
isSuccess()
Signature
public Boolean isSuccess()
Return Value
Type: Boolean