Sub topic 9
DML Governor Limits Apply to Both Approaches
This chapter assumes you can write basic Apex, use try/catch, and know that Apex runs in a transaction. You should also have seen both insert accounts; and Database.insert(accounts, false); in earlier chapters. Here you will see that choosing between them does not change how much DML your transaction is allowed to do.
The limits that matter
Salesforce enforces governor limits per transaction so that no single piece of code can monopolize shared resources. Two of them apply directly to DML.
| Limit | Synchronous Apex | Asynchronous Apex |
|---|---|---|
| DML statements issued | 150 | 150 |
| Records processed by DML | 10,000 | 10,000 |
Check the current Apex Developer Guide for exact values, since Salesforce can change them. The two numbers above are the ones to keep in mind.
The first limit counts DML calls. The second counts the records those calls touch. A transaction can break either one independently. One call with 10,001 records breaks the record limit. One hundred and fifty-one calls with a single record each breaks the call limit.
Related limits also matter once DML is involved:
- Total SOQL queries, because code that loops over DML often loops over queries too.
- CPU time, because triggers, flows, and validation logic fire on every DML call.
- Heap size, because large collections of records are held in memory.
Both approaches use the same counters
A DML statement and a Database method call are two syntaxes for the same operation. Each one increments the same counters.
insert accounts;counts as 1 DML statement.Database.insert(accounts, false);counts as 1 DML statement.Database.insert(accounts, true);also counts as 1 DML statement.
The record count works the same way. If accounts holds 200 records, either form adds 200 to the records-processed total.
Setting allOrNone to false does not reduce the count. A record that fails still counts toward the limit, because Salesforce attempted it. Partial success changes what gets saved. It does not change what gets counted.
What happens when you exceed a limit
Exceeding a governor limit throws a System.LimitException. Two properties of this exception make it different from a DmlException.
- It cannot be caught. A
catch (Exception e)block does not intercept it. The transaction ends and all changes are rolled back. - It is not tied to one record. The whole transaction fails, whichever DML form you used.
This is why allOrNone = false cannot save you from limit problems. That parameter controls how record-level errors such as a missing required field are handled. A limit violation is a transaction-level failure, so it is outside the scope of allOrNone.
// Shows that a limit error is not recoverable with try/catch.
// Run in Anonymous Apex. Expect an uncaught LimitException.
try {
for (Integer i = 0; i < 151; i++) {
insert new Account(Name = 'Account ' + i);
}
} catch (Exception e) {
// Not reached for the limit error.
System.debug('Caught: ' + e.getMessage());
}The loop issues 151 separate DML statements. On the 151st, the transaction fails with "Too many DML statements: 151". The catch block does not run, and none of the 150 earlier inserts is saved.
The same failure occurs if you swap insert for Database.insert(new Account(...), false). The two forms are equal here.
Checking your usage at runtime
The Limits class reports how much of each limit you have consumed. It works the same whichever DML form you use.
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accounts.add(new Account(Name = 'Batch ' + i));
}
System.debug('Before: ' + Limits.getDmlStatements() + ' of ' + Limits.getLimitDmlStatements());
System.debug('Rows before: ' + Limits.getDmlRows() + ' of ' + Limits.getLimitDmlRows());
insert accounts;
System.debug('After insert: ' + Limits.getDmlStatements() + ' statements');
System.debug('Rows after: ' + Limits.getDmlRows());
Database.update(accounts, false);
System.debug('After update: ' + Limits.getDmlStatements() + ' statements');
System.debug('Rows after: ' + Limits.getDmlRows());In a fresh transaction, you should see the statement count go from 0 to 1 to 2, and the row count go from 0 to 200 to 400. The insert statement and the Database.update call add to the same totals.
These methods are useful in debug logs while you tune code, and in defensive checks for unusual cases. In most code you should design to stay far below the limits instead of testing for them.
Triggers share the same budget
DML limits are per transaction, not per Apex class. If your code inserts an Account, and a trigger on Account inserts a Contact, both operations draw from the same pool. The same applies to record-triggered flows and any other automation that runs in the transaction.
This has a practical consequence. A loop that issues 100 DML statements may look safe on its own. If each statement fires a trigger that issues 2 more, the transaction needs 300 statements and fails.
The records-processed count also includes records created by automation. If you insert 200 Accounts and a trigger creates one Contact for each, the transaction has processed 400 records.
Why the limit is about calls, not syntax
A common misconception is that Database methods are "heavier" or "lighter" than DML statements. Neither is true for limits. What changes the count is how many calls you make and how many records each call carries.
Compare two ways to update the same 200 records.
// Pattern A: one call per record. 200 DML statements. Fails at 151.
List<Contact> contacts = [SELECT Id, Title FROM Contact LIMIT 200];
for (Contact c : contacts) {
c.Title = 'Reviewed';
update c;
}// Pattern B: one call for the whole list. 1 DML statement, 200 rows.
List<Contact> contacts = [SELECT Id, Title FROM Contact LIMIT 200];
for (Contact c : contacts) {
c.Title = 'Reviewed';
}
update contacts;Pattern A fails if the query returns 151 or more contacts. Pattern B uses one statement for any list up to the row limit. Changing Pattern A to Database.update(c, false); would not help. The next chapter shows how to restructure code this way in more cases.
Edge cases worth knowing
Empty lists
Passing an empty list to a DML statement or a Database method still counts as one DML statement, even though zero rows are processed. In a loop that calls DML on a possibly empty list, check isEmpty() first.
List<Account> toUpdate = new List<Account>();
// ... logic that may or may not add records ...
if (!toUpdate.isEmpty()) {
update toUpdate;
}Chunking of large lists
Passing a list larger than 10,000 records breaks the row limit, whichever form you use. Salesforce internally processes DML in chunks of 200 records, and triggers run once per chunk. The chunking does not raise your limit. It does mean that a 1,000-record call fires a trigger five times, and those five trigger runs share the same transaction counters.
Mixed sObject types
A single DML call can take a list containing more than one SObject type. Salesforce may count it differently from a call with one type. Because of this, treat lists of mixed types as a case to test, and prefer separate lists for each type when you can.
Mixed DML restrictions
Some setup objects, such as User, cannot be modified in the same transaction as regular objects like Account. This restriction is separate from the count limits, and it applies to both syntaxes. When you hit it, the usual fix is to move one of the operations to asynchronous Apex.
Asynchronous contexts
Batch Apex, Queueable, and @future methods each get their own transaction and their own counters. Moving DML into one of them does not reduce the work, but it does give that work a fresh budget. This is a way to split work that is too large for a single transaction.
Summary of the key points
- Statements and
Databasemethods draw from the same two DML counters. - Failed records and empty lists still use up the budget.
- A
LimitExceptionends the whole transaction and cannot be caught. - Triggers and flows share the budget with your code.
- The way to stay under the limits is to make fewer, larger calls on collections.
Exercise
A developer wraps Database.insert(account, false) in a loop that runs 200 times and expects allOrNone = false to prevent the transaction from failing. What happens, and why?
Show answer
The transaction fails with an uncatchable System.LimitException on the 151st call ("Too many DML statements: 151"), and all earlier changes are rolled back. The allOrNone parameter only controls how record-level errors such as validation failures are handled. Each Database.insert call counts as one DML statement against the per-transaction limit of 150, the same as an insert statement. The fix is to add the records to a list inside the loop and make one Database.insert call after it.
Exercise
You call insert accounts; with a list of 200 Accounts. An Account trigger inserts one Contact per Account in the same transaction. Using the limits above, how many DML statements and how many DML rows has the transaction used, assuming the trigger fires once for the whole list?
Show answer
2 DML statements (one for the Accounts, one for the Contacts created by the trigger) and 400 DML rows (200 Accounts plus 200 Contacts). Both the statement and the row counters are shared by the code and the trigger. The 200-record list is within a single trigger chunk, so the trigger runs once.