+ Start a Discussion
sudhakar reddy 13sudhakar reddy 13 

diff b/w insert and database insert

Muthuraj TMuthuraj T

1.If we use the DML statement (insert), then in bulk operation if error occurs, the execution will stop .and Apex code throws an error which can be handled in try catch block.
 
2.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.
Abhishek BansalAbhishek Bansal
Hi,

The main difference b/w insert and Database.Insert comes into place when you are inserting records in bulk.

Insert - It does not allow you the option of partial success which means that if you use Insert and any single record is failed during insertion than all the other records will be rollbacked and nothing will be inserted.
For Eg : Suppose you are inserting 10 Contacts : 1,2,3,4,5,6,7,8,9,10
Record 1,2,3,4 are successfully inserted but an error occured in record 5 than the execution will stop there only and all the records like 1,2,3,4 are also rollbacked.

Database.Insert - It allows us the option of partial sucess which means that we can define a boolean parameter in database.insert which will identify whether we want partial sucess or not.
Fo Eg : If we are inserting 10 contacts using Database.insert(contactList,true) than it will behave same as insert but if we use database.insert(contactList.false) than all the records which are successful will be inserted and only failed records are rollbacked.

You can more help on Database class in link given below :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm

Regards,
Abhishek