Sub topic 2
DML statements: the concise syntax
This chapter covers the DML statements Apex provides for writing records. It assumes you know basic Apex syntax, SObjects and fields, and try/catch blocks. It also assumes you know that Apex runs in transactions with governor limits.
What DML statements are
A DML statement is a keyword that writes records to the database. Apex has these:
insertcreates new records.updatechanges existing records.upsertinserts or updates, depending on whether the record already exists.deletemoves records to the Recycle Bin.undeleterestores records from the Recycle Bin.
Each statement takes a single SObject or a list of SObjects. A list is one DML statement, no matter how many records it holds.
The core behavior
DML statements are all-or-nothing. If any record in the operation fails, Apex throws a DmlException and rolls back the whole statement. No record from that statement is saved.
You pick the statement that matches your intent. You do not have to configure it. This is why the syntax is called concise: there is nothing to set, and you read the outcome as either "it worked" or "an exception was thrown".
Working through each statement
insert and update
insert assigns an Id to each record. After the call, that Id is available on the same variable, so you can use it right away.
Account acct = new Account(Name = 'Acme');
insert acct;
System.debug(acct.Id); // populated after the insert
acct.Phone = '555-0100';
update acct;Lists
Pass a list to work on many records in one statement.
List<Contact> contacts = new List<Contact>{
new Contact(LastName = 'Smith', AccountId = acct.Id),
new Contact(LastName = 'Jones', AccountId = acct.Id)
};
insert contacts;delete and undelete
delete accepts records or Ids. undelete works only on records still in the Recycle Bin.
delete contacts;
undelete contacts;upsert
upsert decides between insert and update. Without a field specified, it matches on the record Id. The next chapters show how an external ID field changes the match.
Account existing = new Account(Name = 'Acme');
insert existing;
existing.Name = 'Acme Corp';
Account fresh = new Account(Name = 'Globex');
upsert new List<Account>{ existing, fresh };When a record fails
A single invalid record fails the entire statement. This example fails because Name is required on Account.
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme'),
new Account() // Name is required, so this record fails
};
insert accounts;Neither account is saved. The statement throws a DmlException, and the next chapter shows how to catch it.
Governor limits
Every DML statement counts against the per-transaction DML limit, and the records in each one count against the DML rows limit. The statement form does not change these counts. Passing one list is far cheaper than looping and calling insert once per record.
Common mistakes
| Mistake | Result |
|---|---|
| Putting DML inside a `for` loop | Reaches the DML statement limit quickly |
| Using `update` on a record with no Id | Runtime error, because there is nothing to match |
| Expecting valid records to save when one fails | None are saved |
| Using `undelete` on a hard-deleted record | Fails, because the record is no longer in the Recycle Bin |
Exercise
You run insert accounts; on a list of 200 accounts, and one has a blank required field. How many accounts are saved, and what happens in your code?
Show answer
None are saved. The statement throws a DmlException and rolls back all 200 records, so you must catch the exception if you want to continue running your code.
Exercise
Why is for (Account a : accounts) { insert a; } a poor choice compared to insert accounts;?
Show answer
The loop runs one DML statement per record, so it can hit the DML statement limit. The single insert accounts; is one statement for the whole list.