Activity Forums Salesforce® Discussions What is the difference between Insert and Database.insert in Salesforce?

  • Anurag

    Member
    August 3, 2018 at 1:55 pm

    Hi Chanchal,

    Insert – Insert and Database.insert method are same but Database.insert method provide you more flexibility as compared to Insert Method. If there is any exception while making DML using insert then All records will be aborted.

    Database.Insert – Using Database.Insert method you can specify whether you wanted to abort the complete records when there is an error in any records or commit the success record and show a list of failed records. It also provides the Rollback functionality.

     

  • Prachi

    Member
    September 12, 2018 at 5:22 am

    Hi,

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

    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.

    Thanks

  • Parul

    Member
    September 13, 2018 at 4:47 am

    Hi

    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.

    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 1:39 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