Database Class Methods vs DML Statements in Salesforce

Database Class Methods vs DML Statements in Salesforce

How to allow partial execution during DML operation in Salesforce?

ยท

4 min read

In this article, you will learn the difference between Database Class Methods & DML Statements in Salesforce Apex and when to use which one.

Salesforce provides 2 ways to perform DML operations.

  1. DML Statements.

  2. Database Class Methods.

DML Statements

Let's try the below code in the anonymous window.

List<Contact> contacts = new List<Contact>();
contacts.add(new contact(firstname='John',lastname='doe',email='john@testmail.com'));
contacts.add(new contact(firstname='Joy',email='joy@testmail.com'));
insert contacts;

You will get an exception because lastname is a required field in Salesforce and we are trying to insert contact records without lastname. As you can see in the below image.

As you can see above piece of code is less complex and we can easily find the reason for failure because of fewer fields used in the transaction and also we tried it from the anonymous window but what if the code is complex with a lot of fields then we might have to look into logs and find the reason which will be time-consuming. But we can use a try-catch block to handle the exceptions.

Try the below code in the anonymous window.

List<Contact> contacts = new List<Contact>();
contacts.add(new contact(firstname='John',lastname='doe',email='john@testmail.com'));
contacts.add(new contact(firstname='Joy',email='joy@testmail.com'));
try
{
    insert contacts;
}
Catch(DMLException e)
{
    System.debug('An Exception occured while inserting contact record at line number '+e.getlinenumber());
}

This time you will not get an exception but when you open debug logs you will see a message which we provided in the catch block.

In the above image, you can see that an exception occurred hence it confirms that DML statements don't support partial execution which means if an exception occurred then the whole transaction is rolled back. Now we will look at how Database Class Methods solve this problem.

Database Class Methods

Now let's try the same piece of code but this time with Database Class Methods.

List<Contact> contacts = new List<Contact>();
contacts.add(new contact(firstname='John',lastname='doe',email='john@testmail.com'));
contacts.add(new contact(firstname='Joy',email='joy@testmail.com'));
try
{
   Database.SaveResult[] Results = Database.insert(contacts,true);
}
Catch(DMLException e)
{
    System.debug('An Exception occured while inserting contact record at line number '+e.getlinenumber());
}

In the above code, you will see that we have replaced

insert contacts;

with

Database.SaveResult[] Results = Database.insert(contacts,true);

Again you will see that records are not saved in the database and the same error message is printed on debug logs.

It is because in the Database class insert method the second parameter is passed as true which means don't allow partial execution. Hence we need to pass the value as false for partial execution.

Now run the below code from anonymous windows where we passed false.

List<Contact> contacts = new List<Contact>();
contacts.add(new contact(firstname='John',lastname='doe',email='john@testmail.com'));
contacts.add(new contact(firstname='Joy',email='joy@testmail.com'));
try {
    Database.SaveResult[] results = Database.insert(contacts, false);
    for(Database.SaveResult result : results) {
        if(result.isSuccess()) {
            System.debug('Contact inserted :' + result.getId());
        } else {
            for(Database.Error error : result.getErrors()) {
                System.debug('Error occured:' + error.getMessage());
            }
        }
    }
} catch(DMLException e) {
     System.debug('An Exception occured while inserting contact record at line number '+e.getlinenumber());
}

In the above code along with passing false values. I also iterated over results to print the id of the record which are saved successfully and the error message for the records which have failed.

This time you will see an error message for the record which is not inserted and also you will see the id of the record which is created.

Also, you will see a contact record that was created with the name John Doe.

Also, check out the below video on Database Class Methods vs DML Statements in Apex - Salesforce.

Conclusion

In short, we can say that if you want to support the partial execution of records then use Database Class Methods else you can go with DML Statements.

I hope I am able to explain the difference between Database Class Methods vs DML Statements in Salesforce Apex.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here. Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

ย