Trigger architecture
In the same way it is a best practice to have only a single Process Builder process per object, it is also a best practice to have only a single trigger per object. With multiple triggers, we cannot guarantee their order of execution, which may lead to unintended consequences. With a single trigger, however, we can control the order in which the updates are made as the code will run sequentially within the trigger.
A good question I was once asked when teaching a class on Apex was, why one Trigger per object, rather than one Trigger per context per object? That is, why is the suggestion to have an AccountTrigger
and not a series of triggers on the Account
object, where each manages a single context; AccountBeforeInsertTrigger
, AccountAfterUpdateTrigger
, and so on? This can be seen in the following diagram:
It is worth mentioning that this is...