Skip to main content

Code reviews rule: Multiple triggers on the same object

Written by David Martin
Updated this week

Multiple triggers on the same object

Why is this an issue?

Having multiple triggers on the same object causes:

  • Non-deterministic execution order: Salesforce does not guarantee the order in which triggers execute.

  • Duplicate queries: Each trigger may query the same data, wasting SOQL queries.

  • Maintenance difficulty: Logic spread across multiple triggers is harder to understand and debug.

  • Recursion issues: Multiple triggers make it harder to prevent infinite loops.

Examples

Example of incorrect structure (multiple triggers):

force-app/main/default/triggers/
├── AccountValidation.trigger
├── AccountIntegration.trigger
└── AccountNotification.trigger

Example of correct structure (single trigger with handler):

force-app/main/default/triggers/
└── AccountTrigger.trigger

force-app/main/default/classes/
├── AccountTriggerHandler.cls
├── AccountValidator.cls
├── AccountIntegration.cls
└── AccountNotification.cls

How can I fix violations?

Consolidate all triggers for an object into a single trigger that delegates to a handler class:

// Single trigger per object
trigger AccountTrigger on Account (before insert, before update, after insert, after update) {
AccountTriggerHandler handler = new AccountTriggerHandler();

if (Trigger.isBefore) {
if (Trigger.isInsert) handler.beforeInsert(Trigger.new);
if (Trigger.isUpdate) handler.beforeUpdate(Trigger.new, Trigger.oldMap);
}
if (Trigger.isAfter) {
if (Trigger.isInsert) handler.afterInsert(Trigger.new);
if (Trigger.isUpdate) handler.afterUpdate(Trigger.new, Trigger.oldMap);
}
}

When should I disable this rule?

There are no common scenarios where this rule needs to be disabled. Managed package triggers are already excluded automatically.

Resources

Did this answer your question?