Activity › Forums › Salesforce® Discussions › What is the difference between insert() and database.insert() in Salesforce?
Tagged: Database, Database Insert, Difference, DML Statement, RollBack, Salesforce Bulk Insert, Salesforce Development
-
What is the difference between insert() and database.insert() in Salesforce?
Posted by sushant on December 8, 2016 at 2:45 PMHi All,
What is the difference between insert() and database.insert()?
please give suggestions
Thanks
Piyush replied 6 years, 8 months ago 4 Members · 4 Replies -
4 Replies
-
Hi Sushant,
Unlike DML statements, Database methods have an optional allOrNone parameter that allows you to specify whether the operation should partially succeed. When this parameter is set to false, if errors occur on a partial set of records, the successful records will be committed and errors will be returned for the failed records. Also, no exceptions are thrown with the partial success option.
For more information, go to
https://trailhead.salesforce.com/force_com_dev_beginner/apex_database/apex_database_dml
Hope this helps.
-
This reply was modified 9 years, 5 months ago by
Kumar.
-
This reply was modified 9 years, 5 months ago by
- [adinserter block='9']
-
Insert:
- A 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 to the database.
Database.Insert:
- Database methods are static methods available in Database class.
- A partial insert is supported.
- Rollback is supported.
- Includes the optional all-or-none 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.
Log In to reply.