A trigger that works fine in testing and then blows up in production usually has the same cause: it was written for one record and Salesforce ran it on two hundred. Bulkification is not an advanced topic. It is the first thing a trigger has to get right.
One trigger per object
Start with the rule that prevents most trigger messes: one trigger per object. When an object has three triggers, you cannot control the order they run in, and debugging becomes guesswork. One trigger calls a handler class, and the handler decides what happens. Everything else lives in the handler, where it can be tested and reused.
Never query or update inside a loop
This is the mistake that causes the "too many SOQL queries" error every new Apex developer meets. A query inside a loop runs once per record in the batch. Two hundred records means two hundred queries, and the governor limit is one hundred.
The fix is boring and it works: collect the IDs first, query once outside the loop, and store the results in a map.
Set<Id> accountIds = new Set<Id>();
for (Opportunity opp : Trigger.new) {
if (opp.AccountId != null) {
accountIds.add(opp.AccountId);
}
}
Map<Id, Account> accounts = new Map<Id, Account>(
[SELECT Id, Name FROM Account WHERE Id IN :accountIds]
);
Now the loop reads from the map instead of hitting the database. Same pattern for updates: collect the records to change in a list, then run a single DML statement after the loop.
Handle all four contexts
A trigger fires on insert, update, delete, and undelete, and on before and after events. A trigger that only handles after insert will silently do nothing the day someone bulk-loads records with Data Loader, which fires the same events in batches of up to 200. Write the handler to check Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete, and Trigger.isBefore / Trigger.isAfter explicitly, and decide what each context is allowed to do. Before triggers are for validating and changing the records in flight; after triggers are for touching related records.
Respect the order of execution
Triggers do not run in a vacuum. Validation rules, assignment rules, workflow rules, flows, and duplicate rules all run in a defined order, and a trigger that assumes it runs first will be surprised. If a Flow updates a field your trigger reads, the trigger sees the Flow's value on the second pass. Test with realistic data — including the automations already running in the org — not just with a single record in a sandbox.
Test with 200 records, not one
Salesforce runs test methods against your code, but a test that inserts one record proves almost nothing about bulk behavior. Every trigger test should include a bulk test: insert 200 records in one DML statement and assert the results. If the trigger survives that, it survives Data Loader, imports, and mass updates. If it does not, you have found the problem before your users did.
Our guide to writing test classes that actually cover this kind of code is here: Salesforce Test Classes & Code Coverage. And if you are setting up a local development workflow for Apex work, our Claude Code setup guide walks through the terminal, desktop app, and VS Code options.
The short version
One trigger per object, no queries or DML inside loops, explicit handling of every context, and a 200-record test. That is the whole pattern. It is not glamorous, but it is the difference between a trigger that quietly does its job for years and one that pages you on a Friday afternoon.
Originally published at Way2Force — practical guides on Salesforce Flow, automation, and CRM.
Top comments (0)