DML Insert Update Upsert

How to manipulate records with DML.

These are the DML statements available: insert, update, upsert, delete, undelete and merge.

Apex contains the built-in Database class, which provides methods that perform DML operations and mirror the DML statement counterparts. These Database methods are static and are called on the class name:

Database.insert()
Database.update()
Database.upsert()
Database.delete()
Database.undelete()
Database.merge()

DML does not support partial execution, which means if an error is encountered in any one of the records whole transaction will be rolled back. 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, eg: Database.insert(recordList, 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.

By default, the allOrNone parameter is true, which means that the Database method behaves like its DML statement counterpart and will throw an exception if a failure is encountered. The following two statements are equivalent to the insert recordList; DML statement:

Database.insert(recordList);
and
Database.insert(recordList, true);

Learn more here: