Sub topic 3
Why Several Triggers on One Object Cause Trouble
This chapter explains what goes wrong when an object has more than one Apex trigger. It sets up the one-trigger-per-object rule that the next chapters build on.
Prerequisites
This chapter assumes you know basic Apex syntax (classes, methods, collections) and what DML operations are. It also assumes you know the Salesforce governor limits, which are the per-transaction caps on SOQL queries, DML statements and CPU time. You should also know that a trigger runs when records are inserted, updated, deleted or undeleted.
Salesforce allows many triggers per object
Salesforce does not stop you from creating several triggers on the same object and event. The following two triggers are both valid, and both fire on every Account insert.
trigger AccountDefaults on Account (before insert) {
for (Account acc : Trigger.new) {
if (String.isBlank(acc.Description)) {
acc.Description = 'No description';
}
}
}trigger AccountNormalize on Account (before insert) {
for (Account acc : Trigger.new) {
acc.Description = acc.Description.toUpperCase();
}
}Each trigger looks reasonable alone. Together they have a problem. The next section explains it.
Problem 1: execution order is not guaranteed
When several triggers handle the same event on the same object, Salesforce does not define which runs first. The order can differ between orgs, and it can change after a deployment.
In the example above, AccountNormalize calls toUpperCase() on Description. If it runs before AccountDefaults, Description can still be null. The call then throws a NullPointerException and the whole insert fails. If it runs after, the insert succeeds. The same code can pass in a sandbox and fail in production.
Problem 2: logic is scattered
With many triggers, the rules for one object live in many places. To answer "what happens when an Account is inserted?", you must find every trigger on Account and read each one. Developers who add a trigger often do not know what the existing ones do. That makes conflicts like the one above likely.
Problem 3: no shared state or reuse
Separate triggers cannot easily share variables, helper methods or flags. Each one tends to repeat the same loops, queries and checks. Repeated queries also add up. All triggers in one transaction share the same governor limits, so each extra trigger with its own SOQL query uses part of the same budget of 100 queries.
Recursion control is also hard. A guard such as a static variable must be coordinated across every trigger, and nothing forces the triggers to agree on it.
Problem 4: testing and bypassing are harder
You cannot switch off one piece of logic cleanly during a data load or a test. You can only deactivate a whole trigger, and that is a metadata change. With one entry point per object, you can add a single switch that controls all logic for that object. Later chapters build exactly that.
Summary of the trouble
| Problem | Effect |
|---|---|
| Undefined execution order | Behaviour that differs between orgs and deployments |
| Scattered logic | Hard to read, review and change safely |
| No shared code or state | Duplicated work and wasted governor limits |
| No single control point | Hard to bypass, guard and test |
The fix in one sentence
Give each object exactly one trigger. Let that trigger call a single class, which decides what runs and in what order. Order then becomes something you write in code instead of something the platform decides for you.
Exercise
Two before insert triggers on Opportunity both set the Stage field to different values. Which value does the saved record end up with, and why is that a problem?
Show answer
It cannot be predicted. Salesforce does not guarantee the order of triggers on the same object and event, so the last one to run wins, and that can change between orgs or after deployments. The fix is to have one trigger that calls a handler, where the order of the logic is written explicitly.
Exercise
Name two reasons, other than execution order, why a single trigger per object is easier to maintain.
Show answer
Any two of: all logic for the object is found in one place; helper code and static state such as recursion guards are shared; SOQL and DML can be consolidated to save governor limits; one switch can bypass the logic; testing goes through one entry point.