Difference between Insert and Database.Insert in Salesforce
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).