+ Start a Discussion
anjisalesforce@gmail.comanjisalesforce@gmail.com 

What is the difference between Database.insert and Insert?

HI all,

 

Q) What is the difference between Database.insert and Insert?

 

can you please give me rply its very urgent

 

thanks for your help....

Starz26Starz26

it is amazing what a google search will get you

 

 

http://lmgtfy.com/?q=difference+between+database.insert+and+insert+salesforce

 

 

look at the first result

Rocks_SFDCRocks_SFDC

insert is the DML statement which is same as databse.insert. However, database.insert gives more flexibility like rollback, default assignment rules etc. we can achieve the database.insert behavior in insert by using the method setOptions(Database.DMLOptions)

 

Important Difference:

    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.
    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.

 

Thanks,

Anil

Rocks_SFDCRocks_SFDC
Test
suresh.18487suresh.18487
1.If we use the DML statement (insert), then in bulk operation if error occurs, the execution will stop. In that case Apex code throws an error and none of the record is inserted to the database.
 
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. The only record throwing an error will not be inserted / updated.
 
So you should use Database.insert in any bulk dml operation if you are not monitoring the code properly.
amitashtekaramitashtekar

insert will call direct dml operation for record/s and fail the whole operation in case any single record failed to insert.

 

In case of database.insert it accept second parameter to create difference than just insert call.

 

Second parameter decide whether rest of records should be inserted if any record failed to insert.

 

Let me know if you get it or not.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_insert.htm

 

Arun Garg 9Arun Garg 9
Best answer of this question follow this URL.
Insert vs Database.insert() in Salesforce (http://www.salesforceadda.com/2017/08/insert-vs-databaseinsert-in-salesforce.html)