An array of Database.DeleteResult objects is returned with the delete database method. Each element in the DeleteResult array corresponds to the sObject array passed as the sObject[] parameter in the delete Database method; that is, the first element in the DeleteResult array matches the first element passed in the sObject array, the second element corresponds with the second element, and so on. If only one sObject is passed in, the DeleteResult array contains a single element.
The following example shows how to obtain and iterate through the returned Database.DeleteResult objects. It deletes some queried accounts using Database.delete 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 or not for each record. It writes the ID of every record that was processed successfully to the debug log, or error messages and fields of the failed records.
// Query the accounts to delete Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Acme%']; // Delete the accounts Database.DeleteResult[] drList = Database.delete(accts, false); // Iterate through each returned result for(Database.DeleteResult dr : drList) { if (dr.isSuccess()) { // Operation was successful, so get the ID of the record that was processed System.debug('Successfully deleted account with ID: ' + dr.getId()); } else { // Operation failed, so get all errors for(Database.Error err : dr.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 DeleteResult. All are instance methods.
public Database.Error[] getErrors()
Type: Database.Error[]
public ID getId()
Type: ID
If this field contains a value, the object was successfully deleted. If this field is empty, the operation was not successful for that object.
public Boolean isSuccess()
Type: Boolean