Apex offers two ways to perform DML operations:
1. DML statements :
Eg : Insert Acc ;
This is more straightforward to use and result in exceptions that you can handle in your code.
2. Database class methods :
Eg: Database.insert(Acc, false);
By using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. If you specify false for the second parameter and if a record fails, the remainder of DML operations can still succeed.
Instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered.
So how do you choose between the two options available?
If you need the errors that occur during DML to be thrown as an Apex exception that immediately interrupts control flow --> then go for DML statements
If you need to allow partial success of DML. Using the Database class methods, you can write code that never throws DML exception errors. Instead, your code can use the appropriate results array to judge success or failure. --> then go for Database class methods
PS : Database methods also include a syntax that supports thrown exceptions, similar to DML statements.
No comments:
Post a Comment