templates/blog-post-ads.html — every placement in situ — watch the skeletons fill, scroll for the parallax, dismiss the anchor, open the interstitial ← back to docs
Advertisement
Apex

Why your trigger fails at 201 records

Governor limits are per transaction, not per record. Once that lands, bulkification stops being a rule you follow and becomes the only design that makes sense.

Advertisement

Every Apex developer meets this bug once: the trigger that worked all through development, then failed the first time someone imported a spreadsheet.

The limit is the transaction

Governor limits are counted per transaction, not per record. A trigger that runs one query per record does not use one query — it uses as many queries as the data load has rows, and the platform stops it at 101.

NoteThe 101 is not a typo. The hundredth query succeeds; the hundred-and-first is what raises the exception.

Counting the wrong thing

The instinct is to count records. The platform counts statements, and the difference is the entire lesson.

Advertisement

The fix is a pattern

Collect the ids first, query once, put the results in a map, then loop. It is three lines longer and it does not care whether the load is one record or two hundred.

CaseRouter.cls apex
Map<String, Id> queues = queueMap();for (Case c : cases) {  c.OwnerId = queues.get(c.Origin);}
Watch outA map keyed on a field that is not unique silently drops records. Key on the id unless you can prove otherwise.
Advertisement

Testing it honestly

A test that inserts one record proves nothing about the case that broke. Insert two hundred.

TipPut the 200 in a constant your factory reads. When the limit changes, one edit fixes every test.

Next in the series: what actually counts against each limit, and how to read the debug log that tells you.

Advertisement

Swarnil Singhai

Salesforce Architect · 11× certified

Eleven years building on the platform, most of them cleaning up other people's triggers. Writes about the model first and the syntax second.

Advertisement
Ship your first managed packageorgforce.dev Static analysis for Apex, free tierlintforce.io Sandbox seeding in ninety secondsseedbox.app Org monitoring that pages a humanwatchtower.sh
Related

More on Apex

Advertisement
No fillAdd data-collapse and this slot removes itself.
Advertisement
Ads are offMembers read without them — and so do you, right now.

Advertisement
Advertisement