Sub topic 1
Writing records in Apex: the DML operations
Prerequisites
This chapter assumes you already know:
- Basic Apex syntax, including classes, methods, and collections such as
ListandMap. - What SObjects and fields are in Salesforce, such as
AccountandName. - How to use
try/catchblocks in Apex. - That Apex runs inside a transaction that is limited by governor limits.
What DML is
DML stands for Data Manipulation Language. In Apex, DML is the set of operations that write records to the database. Reading records uses SOQL. Writing, changing, and removing them uses DML.
Apex gives you two ways to run the same operations:
- DML statements, such as
insert accounts;. These are keywords built into the language. - Database class methods, such as
Database.insert(accounts, false);. These are static methods on theDatabaseclass.
The rest of this tutorial compares the two. This chapter introduces the operations they share.
The core operations
Both approaches support these operations:
| Operation | What it does | Statement | Database method |
|---|---|---|---|
| insert | Creates new records | `insert accounts;` | `Database.insert(accounts)` |
| update | Changes existing records, matched by ID | `update accounts;` | `Database.update(accounts)` |
| upsert | Inserts or updates, matched by ID or external ID | `upsert accounts;` | `Database.upsert(accounts)` |
| delete | Moves records to the Recycle Bin | `delete accounts;` | `Database.delete(accounts)` |
| undelete | Restores records from the Recycle Bin | `undelete accounts;` | `Database.undelete(accounts)` |
There are also merge (combines duplicate records) and convertLead, which are less common.
A first example with statements
This example runs on a single record and then on a list. Both forms use the same statement.
Account acme = new Account(Name = 'Acme');
insert acme; // acme.Id is now populated
acme.Phone = '555-0100';
update acme;
List<Account> accounts = new List<Account>{
new Account(Name = 'Globex'),
new Account(Name = 'Initech')
};
insert accounts;
delete accounts;
undelete accounts;After a successful insert, Salesforce fills in the Id field on each record you passed. That is why update acme; works on the next line. An update or delete needs an ID, because that is how Salesforce knows which record you mean.
The same work with Database methods
The Database class does the same work.
Account acme = new Account(Name = 'Acme');
Database.insert(acme);
acme.Phone = '555-0100';
Database.update(acme);
Database.delete(acme);
Database.undelete(acme);Used this way, with no extra argument, Database.insert behaves like the insert statement. The difference appears when you pass the optional second argument, allOrNone. Later chapters cover it in detail.
How a transaction fits in
DML does not commit to the database right away. Changes become permanent only when the whole transaction finishes without an unhandled error. If an unhandled exception ends the transaction, Salesforce rolls back every change made in it.
Common errors at this stage
- Updating a record with no ID. Calling
updateon a record you built withnewand never inserted fails, because it has noId. - Missing required fields. Inserting an
Accountwith noNamefails with aREQUIRED_FIELD_MISSINGerror. - Passing a null or an empty list. An empty list is allowed and does nothing. Still, check
isEmpty()first, because every DML call counts against a limit even when it changes no records. - Mixed record types in one list. A list of generic
SObjectvalues can hold different types, but there are restrictions on mixing them in the same call. Use one type per list when you can.
What comes next
The next chapters look at how a DML statement reports failure, then at how Database methods report it differently. The choice between them comes down to how you want errors handled.
Exercise
You build Contact c = new Contact(LastName = 'Smith'); and call update c;. What happens, and what should you do instead?
Show answer
The update fails because c has no Id. It was never saved, so Salesforce cannot tell which record to change. Use insert c; to create it. After that c.Id is set, and update c; works for later changes. If you do not know whether the record exists, upsert handles both cases.