Best Practice: Streamline Multiple Triggers on same Object

Short Description of Advice

Where possible, combine multiple triggers on the same object.

Motivation for Advice

Multiple triggers on the same object often leads to redundant queries being executed, which makes it more likely that governor limits will be exceeded.

Explanation

It is important to avoid redundancies and inefficiencies when deploying multiple triggers on the same object. If developed independently, it is possible to have redundant queries that query the same dataset or possible have redundant for statements.

Note that it is very important to detail exactly how governor limits are applied when multiple triggers are deployed on the same object. For starters, you do not have any explicit control over which trigger gets initiated first. Secondly, each trigger that is invoked does not get its own governor limits. Instead, the first trigger defines the entry point and therefore defines the context of the governor limit calculations. However, all code that is processed, including the additional triggers, share those available resources.

So instead of only the 1 trigger getting a maximum of 20 queries, all triggers on that same object will share those 20 queries. That is why it is critical to ensure that the multiple triggers are efficient and no redundancies exist.

Resources

  • See the Apex Technical Library page for a comprehensive list of resources about Apex Code.
  • See the Database Services Technical Library page for a comprehensive list of resources about the database.
  • See the Apex Developer's Guide to learn about the Apex code and syntax.

Author: Andrew Albert (2009-07-16)

Categories: Logic, Database