Difference between Insert DML statement and Database.Insert Method

Let's look at one of the frequently asked interview questions. What's the diffrence between Inset DML statement and Database.Insert method.

This is one of the most frequently asked interview questions and let me try to take a couple of minutes to answer this.

Both of them in a way serve the same purpose which is nothing but inserting a record, however, they are slight changes in the way they handle the insert operation. Let's look at them.

Insert DML statement

  • When we use a DML statement, if I have a list of multiple records there is no way I can insert the records that are valid and omit the rest of the records that are not valid. Simply put, partial insert is not supported. If an error occurs because of whatever reason, Apex Code throws an error and no record gets saved into the database.
  • The option of Rollback is not supported.

Database.Insert method

  • Insert is a method in a standard class called Database and it's a static method, meaning, we can invoke the method with the notation Database.Insert.
  • Database.Insert supports partial insert.
  • Database.Insert also supports Rollback.
  • Database.Insert Includes an optional allorNone parameters that defaults to true.
  • Database.Insert when used in the bulk operation, in case of an error, the remaining records will be inserted. Only the records throwing an error will not be inserted.

Hope this helps!