DML operations: Apex practice drills
Insert, update, upsert and delete, Database methods, save results, savepoints and bulk-safe DML.
Intermediate · Check and Run with Apex Path ($19 a month).
- SObject field access Beginner · 8 min
Use dot notation when you know the field while you write the code.
- Insert a tagged Account Beginner · 10 min
insert saves a new record and writes the new Id back onto the same variable.
- Update a tagged Account Beginner · 12 min
update saves changed fields on a record that already has an Id.
- Delete challenge-created records Intermediate · 12 min
delete removes records by Id.
- Bulk update once Intermediate · 15 min
Bulk-safe Apex changes records inside a loop and saves them all with one DML after the loop.
- Upsert: insert or update Intermediate · 15 min
upsert inserts when no match exists and updates when one does.
- Handle Database.SaveResult Intermediate · 15 min
Database.insert(records, false) turns on partial success: it returns one SaveResult per record instead of throwing.
- Savepoint and rollback Intermediate · 15 min
A savepoint marks a spot in the current transaction; Database.rollback undoes every DML made after it, with no Recycle Bin trace.
- Insert an Account with Contacts Intermediate · 18 min
A child record needs its parent's Id, so the parent Account is inserted before its Contacts.
- Catch a DmlException and read its details Intermediate · 14 min
Default DML is all-or-none, so one bad row throws DmlException.
- Undelete from the Recycle Bin Intermediate · 12 min
delete moves a row to the Recycle Bin; undelete restores it with the same Id.
- Roll back parent and children on a child failure Intermediate · 15 min
One savepoint before the first DML, one rollback inside the catch, and the whole step vanishes.
- Merge duplicate Accounts Intermediate · 14 min
Database.merge(master, duplicate) keeps the master, deletes the duplicate, and moves related children to the master.