DML Statements and Database Methods – Salesforce Platform Developer 1 Exam Guide
Why DML Statements and Database Methods Matter
Data Manipulation Language (DML) operations are at the heart of nearly every Salesforce application. Whether you are creating records, updating fields, deleting obsolete data, or recovering items from the Recycle Bin, you need DML. The Salesforce Platform Developer 1 (PD1) exam dedicates a significant portion of the Process Automation and Logic domain to this topic because understanding how to persist and manipulate data correctly—and how to handle errors gracefully—is a fundamental skill for every Salesforce developer.
What Are DML Statements?
DML statements are Apex keywords that let you perform record-level operations directly on Salesforce sObjects. The six core DML statements are:
• insert – Creates one or more new records.
• update – Modifies existing records that already have an Id.
• upsert – Inserts or updates records based on a matching field (Id by default, or an external Id).
• delete – Moves records to the Recycle Bin.
• undelete – Restores records from the Recycle Bin.
• merge – Combines up to three records of the same sObject type, keeping one master and deleting the others while re-parenting related records.
Example using a DML statement:
Account acc = new Account(Name = 'Acme Corp');
insert acc;
// acc.Id is now populated
If any record in a DML statement fails, the entire operation is rolled back and an unhandled DmlException is thrown. This is the all-or-nothing behavior of DML statements.
What Are Database Methods?
The Database class provides static methods that mirror every DML statement but give you more control over error handling. Key methods include:
• Database.insert()
• Database.update()
• Database.upsert()
• Database.delete()
• Database.undelete()
• Database.merge()
The critical differentiator is the optional allOrNone parameter (a Boolean). When set to false, the operation uses partial processing: records that succeed are committed, and records that fail are reported in the results without rolling back the entire transaction.
Example using a Database method with partial success:
List<Account> accs = new List<Account>{
new Account(Name = 'Acme'),
new Account() // missing required Name field
};
Database.SaveResult[] results = Database.insert(accs, false);
for (Database.SaveResult sr : results) {
if (!sr.isSuccess()) {
for (Database.Error err : sr.getErrors()) {
System.debug(err.getMessage());
}
}
}
When allOrNone is set to true (or omitted, since true is the default), Database methods behave exactly like DML statements—they throw a DmlException on failure and roll back all records.
How It Works – Key Concepts
1. All-or-Nothing vs. Partial Success
• DML statements are always all-or-nothing.
• Database methods default to all-or-nothing but can be set to allow partial success (allOrNone = false).
2. Return Types
• DML statements return void (or throw an exception).
• Database methods return result objects: Database.SaveResult (insert/update), Database.UpsertResult, Database.DeleteResult, Database.UndeleteResult, and Database.MergeResult. Each result object contains isSuccess(), getId(), and getErrors() methods.
3. Governor Limits
• You can perform a maximum of 150 DML statements per transaction.
• The total number of rows processed by DML is capped at 10,000 per transaction.
• Always bulkify: operate on lists of sObjects rather than single records inside loops.
4. Upsert and External Ids
• upsert matches on the record Id by default. You can specify a custom external Id field: upsert recordList MyExternalId__c;
• If a match is found, the record is updated; otherwise it is inserted.
• If more than one match is found, a DmlException is thrown.
5. Merge Behavior
• merge works only with Lead, Account, Contact, and Case sObjects.
• You can merge up to 3 records at a time (one master + up to two duplicates).
• Related records from duplicates are re-parented to the master; duplicates are deleted.
6. DML on Related Records
• You can insert parent and child records together using external Id relationships in a single DML call, but you cannot cascade inserts using standard Salesforce Ids (the parent must be inserted first to obtain its Id).
7. Transactions and Savepoints
• Use Database.setSavepoint() and Database.rollback(Savepoint sp) to create rollback points within a transaction. This gives fine-grained control without requiring all-or-nothing across the entire execution context.
8. Mixed DML Operations
• You cannot perform DML on setup objects (e.g., User, Group) and non-setup objects (e.g., Account) in the same transaction. The workaround is to use System.runAs() in tests or future methods / Queueable in runtime code.
Comparison Table: DML Statements vs. Database Methods
• Error Handling: DML statements throw DmlException; Database methods can return partial results.
• Partial Processing: Not available with DML statements; available with Database methods (allOrNone = false).
• Return Value: DML statements return nothing; Database methods return result objects (SaveResult, etc.).
• Syntax: DML statements are simpler (insert acc;); Database methods are more verbose (Database.insert(acc, false);).
• When to Use: DML statements when all records must succeed together; Database methods when you need partial success or granular error inspection.
Exam Tips: Answering Questions on DML Statements and Database Methods
Tip 1 – Know the Default Behavior
If a question asks what happens when a Database method is called without specifying the allOrNone parameter, remember the default is true, meaning it behaves identically to the corresponding DML statement (all-or-nothing).
Tip 2 – Partial Success Scenario
Any question about allowing some records to succeed while others fail is pointing you toward Database methods with allOrNone = false. DML statements alone can never do this.
Tip 3 – Identifying Result Objects
Expect questions that ask you to identify the correct return type. Remember: insert and update return Database.SaveResult[]; upsert returns Database.UpsertResult[]; delete returns Database.DeleteResult[].
Tip 4 – Governor Limits in Loops
If you see code with a DML statement inside a for loop, that is almost always the wrong answer. Bulkified code collects records into a list and performs a single DML operation outside the loop.
Tip 5 – Upsert Matching
Questions may test whether you know that upsert uses Id by default, or can use an external Id field. Also remember that if more than one record matches an external Id value, a DmlException is thrown.
Tip 6 – Merge Limitations
Remember the objects that support merge (Lead, Account, Contact, Case) and the maximum of 3 records. If a question mentions merging Opportunity records or merging 5 records, those answers are incorrect.
Tip 7 – Savepoints
Questions about selectively rolling back part of a transaction point to Database.setSavepoint() and Database.rollback(). Note that setting a savepoint counts toward the DML statement limit of 150.
Tip 8 – Mixed DML
If a question describes an error involving setup and non-setup objects in the same transaction, the correct solution typically involves separating the operations into different execution contexts (e.g., a future method).
Tip 9 – Read the Code Carefully
Many exam questions present small code snippets. Pay close attention to whether the code uses a DML statement (e.g., insert) or a Database method (e.g., Database.insert()), and whether the allOrNone parameter is present and set to true or false.
Tip 10 – Try-Catch with DML Statements
DML statements can be wrapped in try-catch blocks to handle DmlException. However, even with a try-catch, the DML operation itself is still all-or-nothing. Partial success is only possible with Database methods and allOrNone = false.
Summary
Mastering DML statements and Database methods requires understanding three core ideas: what operations are available, how errors are handled, and what governor limits apply. DML statements offer a concise, all-or-nothing approach. Database methods extend that with optional partial processing and detailed result objects. For the PD1 exam, focus on recognizing when each approach is appropriate, knowing the return types, understanding bulkification, and being aware of special behaviors like merge limitations, upsert matching, mixed DML restrictions, and savepoint usage. With these concepts firmly in hand, you will be well-prepared to tackle any exam question on this topic.