Activity Forums Salesforce® Discussions What is the difference between insert() and database.insert() in Salesforce?

  • What is the difference between insert() and database.insert() in Salesforce?

  • sushant

    Member
    December 8, 2016 at 2:45 pm

    Hi All,

    What is the difference between insert() and database.insert()?

    please give suggestions

    Thanks

  • Kumar

    Member
    December 9, 2016 at 6:54 am

    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 4 years, 11 months ago by  Kumar.
  • sushant

    Member
    December 12, 2016 at 2:24 pm

    Thanks Kumar

  • shariq

    Member
    July 31, 2017 at 6:29 am

    Hi Sushant,

    You can refer to this link.

    Hope this helps.

  • Piyush

    Member
    August 19, 2019 at 6:05 am

    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.
Viewing 1 - 5 of 5 posts

Log In to reply.

Popular Salesforce Blogs