Newer Version Available
DeleteResult Class
Namespace
Usage
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.
Example
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.
1// Query the accounts to delete
2Account[] accts = [SELECT Id from Account WHERE Name LIKE 'Acme%'];
3// Delete the accounts
4Database.DeleteResult[] drList = Database.delete(accts, false);
5
6// Iterate through each returned result
7for(Database.DeleteResult dr : drList) {
8 if (dr.isSuccess()) {
9 // Operation was successful, so get the ID of the record that was processed
10 System.debug('Successfully deleted account with ID: ' + dr.getId());
11 }
12 else {
13 // Operation failed, so get all errors
14 for(Database.Error err : dr.getErrors()) {
15 System.debug('The following error has occurred.');
16 System.debug(err.getStatusCode() + ': ' + err.getMessage());
17 System.debug('Account fields that affected this error: ' + err.getFields());
18 }
19 }
20}DeleteResult Methods
The following are methods for DeleteResult. All are instance methods.
getErrors()
Signature
public Database.Error[] getErrors()
Return Value
Type: Database.Error[]
getId()
Signature
public ID getId()
Return Value
Type: ID
isSuccess()
Signature
public Boolean isSuccess()
Return Value
Type: Boolean