+ Start a Discussion
Mayank_SareenMayank_Sareen 

insert() and database.insert() -- how to choose?

Hi all,

I have a quick question, I am not able to understand when exactly do we use 'insert()' and 'database.insert()'... both seem to do the work of insertion in the database. 

Thanks
Mayank.
 
Best Answer chosen by Mayank_Sareen
Shubham NandwanaShubham Nandwana
Hi Mayank,

Insert:
-The partial insert is not supported.
-Rollback is not supported.
-If we use the DML statement (Insert) in bulk operation, then if an error occurs the execution will stop. In that case, Apex code throws an error and none of the records will insert into the database.

Database.Insert
-Database methods are static methods available in Database class.
-The partial insert is supported.
-Rollback is supported.
-Includes the optional allorNone parameters that default true.

If we use DML database methods (Database.Insert) in bulk operation, then if an error occurs the remaining records will be inserted/updated means partial DML operation will be done. The only record throwing an error will not be inserted/updated.

Example: If we are inserting 10 records in an object, Where 5 records are correct and the remaining 5 records are incorrect.
In DML statement (Insert) all the 10 records will be failed, because if one record is incorrect or error means all other remaining records will not be inserted. It will throw an error.
In Database. insert 5 records will be inserted, the remaining 5 records will be failed.(i.e. Partial DML Operation).

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts

All Answers

Shubham NandwanaShubham Nandwana
Hi Mayank,

Insert:
-The partial insert is not supported.
-Rollback is not supported.
-If we use the DML statement (Insert) in bulk operation, then if an error occurs the execution will stop. In that case, Apex code throws an error and none of the records will insert into the database.

Database.Insert
-Database methods are static methods available in Database class.
-The partial insert is supported.
-Rollback is supported.
-Includes the optional allorNone parameters that default true.

If we use DML database methods (Database.Insert) in bulk operation, then if an error occurs the remaining records will be inserted/updated means partial DML operation will be done. The only record throwing an error will not be inserted/updated.

Example: If we are inserting 10 records in an object, Where 5 records are correct and the remaining 5 records are incorrect.
In DML statement (Insert) all the 10 records will be failed, because if one record is incorrect or error means all other remaining records will not be inserted. It will throw an error.
In Database. insert 5 records will be inserted, the remaining 5 records will be failed.(i.e. Partial DML Operation).

Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Mayank,
 
1. Major Difference between  Database.insert and Insert is :

    If we use the DML statement (insert), then in bulk operation if the error occurs, the execution will stop and Apex code throws an error which can be handled in try catch block.
    If DML database methods (Database.insert) used, then if error occurs the remaining records will be inserted/updated means partial DML operation will be done.
    
2.also check the following link  :

http://www.salesforceadda.com/2017/08/insert-vs-databaseinsert-in-salesforce.html

Hope this solution helps.

Thank You
Ajay Dubedi