DEV Community

bhanukarkra
bhanukarkra

Posted on

DML in Apex

insert, update, upsert, delete, undelete, merge

Each DML statement accepts either a single sObject or a list (or array) of sObjects (more efficient.)

The merge statement merges up to three records of the same sObject type into one of the records, deleting the others, and re-parenting any related records.

Upsert
compares value of one field (user defined) or ID to check for insertion or update.

*If the key is not matched, a new object record is created.
*If the key is matched once, the existing object record is updated.
*If the key is matched multiple times, an error is generated and the object record is neither inserted or updated

Database methods (built in)
Database.insert()
Database.update()
Database.upsert()
Database.delete()
Database.undelete()
Database.merge()
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, 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.

Database.insert(recordList, false); //allornone set to false.
Default is true.

https://trailhead.salesforce.com/content/learn/modules/apex_database/apex_database_dml?trailmix_creator_id=shibua&trailmix_slug=ust-sfdc-dev-apex-vf-basics

Top comments (0)