All articles

Platform

Too many DML statements: 151 in Apex

Move repeated DML out of loops, inspect shared automation and verify bulk behavior without relying on asynchronous headroom.

A DML statement inside a record loop can turn one business operation into hundreds of database operations. Collect the records first, then write the collection once.

The governor reference lists 150 DML statements for both synchronous and asynchronous transactions. Moving the same loop into a Queueable job doesn’t increase that allowance. Other resource limits still apply when the statements are consolidated.

Change the shape of the write

This illustrative fragment assumes accountIds contains valid Account IDs and that creating these Contacts is an authorized operation. It demonstrates collection-based DML, not a complete production service.

List<Contact> contactsToInsert = new List<Contact>();
for (Id accountId : accountIds) {
    contactsToInsert.add(new Contact(
        AccountId = accountId,
        LastName = 'Primary contact'
    ));
}
if (!contactsToInsert.isEmpty()) {
    insert contactsToInsert;
}

The list is written outside the loop. In production, use real business inputs, enforce the relevant permissions and decide how record-level errors should be reported. The pattern doesn’t bypass required fields, duplicate rules or the separate DML row limit.

Follow the save beyond your class

A clean method can invoke automation that issues repeated writes elsewhere. Trace the flows, triggers and service calls that the operation starts. The exception identifies where the limit was crossed; that location may not be where most statements were spent.

The Limits class provides getDmlStatements() and getLimitDmlStatements(). Measure before and after the suspected boundary. Avoid an assumption that every statement comes from an insert keyword: savepoints, rollback and other documented methods also affect the counter.

For Flow, examine data operations inside loops and build collections where appropriate. Salesforce’s bulk execution behavior depends on how interviews and elements execute, so inspect the actual transaction rather than assuming every diagram produces a fixed count.

Protect the fix with a bulk test

Create a representative record collection in test setup. Use Test.startTest() before the operation under test, then assert both the saved records and the relevant resource usage. Include empty input and a mixed set that triggers different branches.

Don’t write a test that only asserts the counter is below the platform maximum. It could pass after a large regression. Define a reasonable expected bound for the specific operation, allowing for the automation your test intentionally invokes.

For reusable architecture, centralize writes at a clear service boundary. The bulk-trigger pattern is a useful starting point. A Unit of Work can help coordinate writes, but it still needs correct dependency ordering and deliberate handling of failures.

Keep reading

All articles