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

  • Laveena

    Member
    August 23, 2019 at 5:31 am

    Hi Hariom,

    Insert:

    • Partial insert is not supported.
    • Roll back is not supported.
    • If we use the DML statement (Insert) in bulk operation, then if error occurs the execution will stop. In that case Apex code throws an error and none of the record will insert to the database.
      Database.Insert:
    • Database methods are static methods available in Database class.
    • Partial insert is supported.
    • Roll back is supported.
    • Includes the optional allorNone parameters that defaults true.
    • If we use DML database methods (Database.Insert) in bulk operation, 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.

    Thanks

  • Prachi

    Member
    August 23, 2019 at 5:32 am

    Hi,

    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.

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos