The following example obtains and iterates through the returned Approval.LockResult objects. It locks some queried accounts using Approval.lock 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 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()); } } }
The following are methods for LockResult.
public List<Database.Error> getErrors()
Type: List<Database.Error>
public Id getId()
Type: Id
If the field contains a value, the object was locked. If the field is empty, the operation was not successful.
public Boolean isSuccess()
Type: Boolean