Newer Version Available
SaveResult Class
Namespace
Usage
An array of SaveResult objects is returned with the insert and update database methods. Each element in the SaveResult array corresponds to the sObject array passed as the sObject[] parameter in the Database method, that is, the first element in the SaveResult 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 SaveResult array contains a single element.
A SaveResult object is generated when a new or existing Salesforce record is saved.
Example
The following example shows how to obtain and iterate through the returned Database.SaveResult objects. It inserts two accounts using Database.insert with a false second parameter to allow partial processing of records on failure. One of the accounts is missing the Name required field, which causes a 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. This example generates one successful operation and one failure.
1// Create two accounts, one of which is missing a required field
2Account[] accts = new List<Account>{
3 new Account(Name='Account1'),
4 new Account()};
5Database.SaveResult[] srList = Database.insert(accts, false);
6
7// Iterate through each returned result
8for (Database.SaveResult sr : srList) {
9 if (sr.isSuccess()) {
10 // Operation was successful, so get the ID of the record that was processed
11 System.debug('Successfully inserted account. Account ID: ' + sr.getId());
12 }
13 else {
14 // Operation failed, so get all errors
15 for(Database.Error err : sr.getErrors()) {
16 System.debug('The following error has occurred.');
17 System.debug(err.getStatusCode() + ': ' + err.getMessage());
18 System.debug('Account fields that affected this error: ' + err.getFields());
19 }
20 }
21}SaveResult Methods
The following are methods for SaveResult. All are instance methods.
getErrors()
Signature
public Database.Error[] getErrors()
Return Value
Type: Database.Error[]
getId()
Signature
public ID getId()
Return Value
Type: ID
Versioned Behavior Changes
In API version 53.0 and later, the method returns the sObject ID. However, if record locking fails during the update operation, the method returns a null value.
In API version 52.0 and earlier, the method returned a null value if the record wasn’t updated successfully.
isSuccess()
Signature
public Boolean isSuccess()
Return Value
Type: Boolean
Example
This example shows the code used to process duplicate records, which are detected when there is an unsuccessful save due to an error. This code is part of a custom application that implements duplicate management when users add a contact. See DuplicateResult Class to check out the entire sample applicaton.
1if (!saveResult.isSuccess()) { ... }