Database vs DML

We often talk about DML operations in Salesforce. Almost all the customization’s in salesforce uses DML operations directly or indirectly.

DML stands for Data Manipulation Language, and it is used to manipulate the data in our Database. It includes Insertion, updation and deletion of records.

But, along with DML, Salesforce has another class that serves the same purpose as DML. Yes, you heard it right and they are known as DATABASE Classes. These classes are salesforce internal classes and can be accessed easily.

Now, the question is why to use Database class methods when we already have DML’s. So, the answer to your question is, Database methods doesn’t only insert or update or delete the records to and from database but also provides more control and visibility of the data that it processes. Here, we are going to understand the difference between these two in details

DML operations follows roll back principle. It means they will either finish the whole operation or revert everything to square one. It can be tedious sometimes if we are operating on large set of data and because of few records the whole transactions gets recalled.

Well, now you don’t have to worry anymore about such issues, here comes Database class methods for your rescue. Unlike DML’s, Databases methods are designed in such a way that if some of your record fails, your transaction will not be reverted and it will continue the operation with valid records. In simple terms i can say, they follows partial insert, update and delete. You can also retrieve the list of data which are inserted and those which fails. Along with this interesting feature, if you want to follow complete rollback, you can set that as well in database methods. Intrigue !! isn’t it ? 🙂

Let’s understand its behavior with a simple example. Let’s imagine you have Student custom object and it is in Master details relation with Contact. You have a requirement where you want to insert some Students records.

Code Snippet 1→

Contact con =New contact(firstname=’Raushan’, lastname=’Singh’);

Insert con;

List<student__c> insertstu=New List<student__c>();

Student__c stu =New Student__c(); // Initialize student object;

stu.Name=’Raushan Kumar’;

stu.contact__c=con.id; //contact__c is the field which connects to Contact;

insertstu.add(stu);

student__c stu1=New student__c();

stu1.Name=’John’;

//Lets Not associate ‘john’ with any contact and add to the List;

insertstu.add(stu1);

Insert insertstu;

Once you execute the above code snippet, you will see none of the records has been inserted. Now, Let’s change the insert to database.Insert .

Database.Insert(insertstu,false); // We will talk more about the Second parameter in some time.

Database.insert will commit one student record to the database and will keep a track of both the success and the failed record(In the example its John record).if you want, you can retrieve the list of both failed and success records.

Let’s dive more deeper and tries to understand the structure of database.insert method below.

Code Snippet 2 →

Public static database.saveresult insert(sObject recordtoinsert, Boolean allornone){

// salesforce defined logic;

}

As you can see in the above code snippet, Database.insert method returns committed and uncommitted records in database.saveresult data type. Boolean allornone determines whether you want to follow complete rollback or not. If set yes, it will either commit everything or nothing. If Set to False, it will follow partial operation. Rest, the structure looks pretty simple and easy to understand.

Let’s make some changes in our Previous code and see how we can retrieve and process the list of failed and Passed records using database.saveresult.

In the continuation of Code Snippet 1 →

List<database.saveresult> sturecords=database.insert(insertstu,false);

‘sturecords’ list will store the list of passed and failed records. Let’s understand how we can access them,

for(database.saveresult st:sturecords){

if(st.isSuccess()){

system.debug(‘The success record ids are’+st.getid);

}

else

for(Database.error error : st.geterror()){

system.debug(‘Failed record with message’+error.getmessage());

system.debug(‘Field causing issue ’+error.getfields());

}

}

So, now you know how to use these database methods and leverages its cool features and make your development faster and better. Similar to Database.Insert, you can also implement other database methods like database.update, database. delete and many more.

I hope this blog has served your purpose.

Thank you