In Salesforce, when you perform operations like inserting, updating, or deleting records, you use the Database.SaveResult class to capture the results. If something goes wrong during these operations, exceptions are thrown, and the entire operation is rolled back.
However, when you use methods from the Database class, there’s a different behavior. These methods allow for partial success, meaning some records may succeed while others fail. Instead of throwing exceptions, they return a list of errors for the failed records.
To access these errors, you use the getErrors() method, which returns a list of Database.Error objects containing detailed information about the failures. This allows developers to handle failed records more effectively by retrieving specific error details and taking appropriate actions.
Syntax for Batch:
Database.SaveResult[] saveResults = Database.insert(recordsToInsert, false);
for(Database.SaveResult sr : saveResults)
{
if (sr.isSuccess())
{
System.debug(‘Record with Id ‘ + sr.getId() + ‘ was inserted successfully.’);
}
else {
for(Database.Error err : sr.getErrors()) {
System.debug(‘Error message: ‘ + err.getMessage());
}
}
}
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.
Account[] accts = new List<Account>{
new Account(Name=’Account1′),
new Account()};
Database.SaveResult[] srList = Database.insert(accts, false);
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
]
System.debug(‘Successfully inserted account. Account ID: ‘ + sr.getId());
}
Else
{
for(Database.Error err : sr.getErrors())
{
System.debug(err.getStatusCode() + ‘: ‘ + err.getMessage());
}
}
}
Advantages of using Database.SaveResult
The Database.SaveResult class in Salesforce provides several benefits when working with DML operations. Here are some advantages of using Database.SaveResult:
Error Handling: Database.SaveResult allows you to handle errors gracefully. When performing DML operations, there can be various reasons for records to fail, such as validation rules, triggers, or duplicate rules. With Database.SaveResult, you can easily identify which records failed and access the corresponding error messages to take appropriate action.
Id Access: Database.SaveResult provides access to the IDs of the successfully saved records. You can use saveResult.getId() to retrieve the ID of each saved record. This is useful when you need to perform further operations or establish relationships with other records based on the saved IDs.
Methods of Database SaveResult
In Salesforce, the Database.SaveResult class provides several methods to access and handle the results of a database operation. Here are the commonly used methods available in the SaveResult class:
isSuccess(): Returns a Boolean value indicating whether the operation was successful (true) or not (false).
getId(): Retrieves the ID of the successfully saved record. This method is useful for accessing the ID of an individual record.
getErrors(): Returns an array of Database.Error objects representing the errors associated with a failed record. You can iterate over this array to access the error messages and details.