• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How To Use Database SaveResult In Salesforce Cloud

  • April 5, 2024
  • Tech Blogger
  • Uncategorized
  • 0

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.

 

Author Bio

Tech Blogger
+ Recent Posts
  • Comprehensive Overview of Salesforce Agent-Force Platform
    June 12, 2025
    Comprehensive Overview of Salesforce Agent-Force Platform
  • Salesforce GraphQL Revolutionizing Data Queries in CRM
    June 6, 2025
    Salesforce GraphQL: Revolutionizing Data Queries in CRM
  • Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
    May 29, 2025
    Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
  • Einstein Activity Capture Boosting Productivity & Relationship Intelligence
    May 22, 2025
    Einstein Activity Capture: Boosting Productivity & Relationship Intelligence
Tags: Salesforce Development & Implementation
  • Previous Why Salesforce Developers Can Rest Easy: How AI Won’t Replace Their Jobs
  • Next How To Use Transform (Beta) Element In Salesforce Flow
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Comprehensive Overview of Salesforce Agent-Force Platform
Comprehensive Overview of Salesforce Agent-Force Platform June 12, 2025
Salesforce GraphQL Revolutionizing Data Queries in CRM
Salesforce GraphQL: Revolutionizing Data Queries in CRM June 6, 2025
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025

Copyright @2023 Merfantz Technologies, All rights reserved