All You Need to Know About Database Class Methods to Perform DML Operations
In this blog, we will discuss Database class methods to perform DML operations. So let's get to the topic.
- Apex contains the built-in database class.
- Database classes provide methods that can perform DML operations.
- Database class methods are static and called through the name of the class.
- Database.insert()
- Database.update()
- Database.upsert()
- Database.delete()
- Database methods have an optional allorNone parameter.
- This parameter allows you to specify whether the operation should partially succeed.
- When this parameter is set to false, if an error occurs on a partial set of records, then the successful records will be committed and errors will be returned for the failed records.
- No exceptions are thrown with the partial success option.
- This feature is not available with DML statements.
Don't forget to check out: DML Statements vs Database Methods in Salesforce
Example
Database.insert ( recordList,false);
The database methods return result objects containing the success or failure information of each record.
Database.insert and Database.update
For insert & update operations an array of Database.SaveResult objects are returned.
Database.SaveResult results [] = Database.insert(recordList, false);
Let's take an example to understand this:
//create a list of contacts List<Contact> contactList = new List<Contact> { new Contact(FirstName=’name1’,LastName=’person1’), new Contact(FirstName=’name2’,LastName=’person2’), new Contact()}; //Bulk insert all contacts with one DML call Database.SaveResult[] srList = Database.insert(contactList, false); //Iterate through each returned result For (Database.SaveResult sr : srList) { if( sr.isSuccess()) { //Operation was successful, so get the ID of the record that was processed System.debug(‘contact inserted’ + sr.getID()); } else { //operation failed, so get all errors for(Database.Error err : sr.getErrors()) { System.debug(err.getStatusCode() + err.getMessage()); System.debug(‘Contact fields that affected this error:’ + err.getFields()); } } }
Database.upsert and Database.delete
- Database.upsert( recordList,false);
- Database.UpsertResult results [] = Database.upsert(recordList,false);
- Database.delete( recordList, false);
- Database.DeleteResult results [] = Database.delete(recordList,false);
- By default allorNone parameter is true, so these all are same:
- Insert contactList; OR, Databaseinsert(contactList), OR Database.insert(contactList,true)
Check out another amazing blog by Bhawana here: DML Operations in Apex: All You Need to Know
What to Use DML Statement or Database Methods?
- Use DML statements if you want to throw errors through apex exceptions and handle them with try and catch block.
- Use Database methods if you want to allow the partial success of a bulk operation. In this way, successful records will be committed and errors will be returned for the failed records.
- Database methods can also throw exceptions similar to DML statements.
This is all about Database class methods to perform insert, update, upsert, and delete operations in apex. I hope this information is helpful to you.
Responses