Activity Forums Salesforce® Discussions What is the difference between the normal “insert” operation and “Database.insert” operation in Salesforce?

  • Rajan

    Member
    August 20, 2018 at 10:11 am

    Assume that you are inserting 100 records. If any one of the records fails due to error then entire operation will fail. None of the records will be saved into the database. With insert/update if we use try-catch then we can capture only one error which will cause to stop the operation. Assume that you are inserting 100 records. If any one of the records fails due to error then it will perform partial operation (valid records will be inserted/updated) if we use
    Database.insert(list,false)/ Database.update(list,false). With Database.insert/Database.update we can capture all the errors by saving result in Database.saveResult[].

  • Parul

    Member
    September 13, 2018 at 7:23 pm

    Hi

    Insert Operation:

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

    Example: If we are inserting 10 records in an object, Where 5 records are correct and remaining 5 records are incorrect.

    In DML statement (Insert) all the 10 records will be failed, because if one record is incorrect or error means all other remaining records will not be inserted. It will throw error.
    In Database.insert 5 records will be inserted, remaining 5 records will be failed.(i.e. Partial DML Operation).

     

    Thanks

  • shariq

    Member
    September 14, 2018 at 12:44 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.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos