Sub topic 4
Database class methods: the same operations as method calls
This chapter assumes you know basic Apex syntax, SObjects and collections, and try/catch. It also assumes you know that Apex runs in transactions with governor limits. The previous chapters covered DML statements and DmlException. Here you meet the second way to write records.
The Database class
The Database class offers a static method for each DML operation. Each method does the same work as its statement.
| DML statement | Database method |
|---|---|
| `insert records;` | `Database.insert(records)` |
| `update records;` | `Database.update(records)` |
| `upsert records;` | `Database.upsert(records)` |
| `delete records;` | `Database.delete(records)` |
| `undelete records;` | `Database.undelete(records)` |
Every method takes a single SObject or a list of SObjects. Each also has an overload that takes a Boolean allOrNone parameter. Most also have an overload that takes a Database.DMLOptions object. A later chapter covers DMLOptions.
The default behavior matches the statements
If you call Database.insert(records) with no second argument, allOrNone defaults to true. The method then behaves like the insert statement. If any record fails, the whole operation rolls back, and Apex throws a DmlException.
The difference appears when you pass false:
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme'),
new Account() // Name is required, so this record fails
};
List<Database.SaveResult> results = Database.insert(accounts, false);
System.debug(results.size()); // 2With allOrNone set to false, the method saves the valid record. It does not throw for record-level errors. Instead it returns one result object per input record, in the same order as the input list.
What the methods return
The return type depends on the operation.
| Method | Returns |
|---|---|
| `Database.insert`, `Database.update` | `List<Database.SaveResult>` |
| `Database.upsert` | `List<Database.UpsertResult>` |
| `Database.delete` | `List<Database.DeleteResult>` |
| `Database.undelete` | `List<Database.UndeleteResult>` |
The next chapters show how to read these results. For now, note that isSuccess() tells you whether a record was saved, and getErrors() returns the details when it was not.
Common errors
- Assuming a failure throws. With
allOrNoneset tofalse, a record-level error does not raise an exception. If you never read the results, failures go unnoticed. - Assuming the whole batch rolled back. With
false, records that passed validation are saved even though others failed. - Expecting no exceptions at all. Some problems still throw, even with
false. An example is passing a null list or hitting a governor limit. Wrapping the call intry/catchis still sensible.
A reminder on limits
A Database method call counts as one DML statement against the governor limit, and the records in its list count toward the rows limit. This is the same as the statement form. Calling it inside a loop is as costly as writing insert inside a loop.
Exercise
You call Database.update(contacts, false) on a list of 10 contacts. Three contacts fail validation. What is saved, what is thrown, and how many results come back?
Show answer
The seven valid contacts are saved. No DmlException is thrown for the record-level failures. The method returns a list of 10 SaveResult objects, in the same order as the input list, and three of them report isSuccess() as false.