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

4 réponses
  1. 27 août, 05:38

     Hi, I would use normal DML when the operation must be atomic and either all records should succeed or none should. If one record fails, the entire DML operation fails and none of the records are committed.  

    I would use Database.insert(records, false) for bulk processing where individual records can fail independently for example, integrations or data imports. It'll help to inspect Database.SaveResult for each record, capture the record's identifier and detailed Database.Error information, log failures persistently, and allow successful records to continue processing.

    In this case Salesforce attempts to insert every record independently: 

    •  Successful records are committed. 
    •  Failed records are not committed. 
    •  You get a Database.SaveResult for each input record. 
    •  The Apex transaction itself doesn't fail simply because some records failed.
0/9000