Skip to main content

Hi Everyone, 

 

I’m working with bulk DML operations in Apex and want to understand the practical difference between using standard DML statements and Database methods with allOrNone = false. 

 

In a real project, when would you prefer Database.insert(records, false) over a normal insert records statement? What are the best practices for handling and logging failed records using Database.SaveResult? 

 

Thanks in advance!  

 

#Trailhead  #Apex  #Salesforce Developer

5 respuestas
  1. 4 sept, 04:38

    @deeFor real projects, Database.insert(records, false) is useful when you want partial success—one failed record should not prevent valid records from being inserted.

    Database.SaveResult[] results = Database.insert(records, false);

    for (Integer i = 0; i < results.size(); i++) {

    if (!results[i].isSuccess()) {

    for (Database.Error err : results[i].getErrors()) {

    System.debug(

    'Failed Record: ' + records[i].Id +

    ' | Error: ' + err.getMessage()

    );

    }

    }

    }

    Use normal insert when the entire operation should succeed or fail together.

    Use Database.insert(records, false) for integrations, data loads, or batch processing where you want successful records saved while capturing failures for logging/reprocessing.

    Best practice: always process SaveResult[] and log the record ID, status code, and error message for failed records. 

0/9000