Sub topic 4
The one-trigger-per-object rule
This chapter assumes you can read basic Apex (classes, methods, collections) and know what DML operations are. It also assumes you know that Salesforce enforces governor limits per transaction. Earlier chapters covered trigger events and context variables. Here you will see why one trigger per object is the standard rule, and what it looks like in code.
The rule
An object should have exactly one Apex trigger. That trigger subscribes to every event you might need. Everything it does is passed to a class, which later chapters build out.
Salesforce does not stop you from creating a second trigger on the same object. The rule is a convention. It exists because of what happens when you break it.
What goes wrong with two triggers
Suppose two developers each add a trigger to Account for the same event:
trigger AccountDefaults on Account (before insert) {
for (Account acc : Trigger.new) {
if (acc.Rating == null) {
acc.Rating = 'Warm';
}
}
}trigger AccountRatingRules on Account (before insert) {
for (Account acc : Trigger.new) {
if (acc.AnnualRevenue != null && acc.AnnualRevenue > 1000000) {
acc.Rating = 'Hot';
}
}
}Both triggers write to Rating on the same record, so the final value depends on which one runs last. Salesforce does not guarantee the order in which triggers on the same object and event execute. It may run them in one order today and another order after a deployment. The result can change without any code change on your side.
Other problems follow from the same cause:
- Shared limits. Both triggers run in one transaction. They share one pool of SOQL queries, DML statements, and CPU time. Each trigger looks fine alone, but together they can exceed a limit that neither one planned for.
- No single place to look. To learn what happens when an Account is inserted, you must find every trigger on Account. Nothing in the code tells you how many there are.
- No shared control. A recursion guard or bypass switch (covered in later chapters) only works if all logic goes through one entry point.
The single trigger
The fix is one trigger per object, subscribed to all seven events:
trigger AccountTrigger on Account (
before insert, before update, before delete,
after insert, after update, after delete, after undelete
) {
new AccountTriggerHandler().run();
}Subscribing to every event costs almost nothing. An event with no matching handler method simply does nothing. If you need after delete later, you add a method to the handler. You do not create a new trigger, so the order of execution stays something you control in code.
The order of work is now explicit. The handler decides which method runs and in what sequence, so the two rating rules above become two steps in one method. Their order is visible and stays the same.
Common errors
- Adding "just one more" trigger. A new feature gets its own trigger because the existing one looks risky to touch. Add a method to the handler instead.
- Assuming you own all triggers. Triggers from managed packages are installed alongside yours. You cannot merge them into your trigger, but you can keep your own code in one trigger so that it is the only part you must reason about.
- Putting logic in the single trigger. One trigger with 300 lines of loops is the same problem in a different file. The trigger stays logic-less, and the next chapter shows how.
Exercise
Two triggers on Contact both handle before update. One sets Title, and the other clears Title when it contains a certain word. A tester reports that Title is sometimes set and sometimes blank after the same update. What is the cause, and how does the one-trigger-per-object rule fix it?
Show answer
Salesforce does not guarantee the execution order of multiple triggers on the same object and event, so whichever trigger runs last decides the final value of Title. With one ContactTrigger that calls a handler, both rules become steps in one method. They run in the order you wrote them every time.