man coding

APEX: How-To Avoid SOQL101 Issues Using Advanced Trigger Pattern

clervalfokle Applications, Blog, Salesforce

Who should read this?

This blog entry will be useful for:

  • any advanced developer or architect
  • beginner or intermediate developers interested in advanced apex development
  • technical consultants in the role of Architect/Senior Developer, working on projects involving multiple developers in environments with complex business logic involving either several objects in either Sales or Service cloud or integrated environments

Why should you read this?

This blog offers a framework/pattern, which, when properly implemented would give developers control over trigger execution flow, helping them avoid the trigger looping. This then will reduce the risk of experiencing SOQL 101 issues.

The Goal of this Article:

Have you ever come across a SOQL101 error while running tests or while saving data?

If yes, you probably would agree that SOQL101 is a very unpleasant, process disrupting issue that is actually a direct consequence of the design; we all deal with it differently, depending on the level of complexity of the application. The root cause for the SOQL101 symptom varies from improperly bulkified data (data transformed and put in a list to be affected by DML, all at once) or when all best practices are put in use in apex development, the root cause tends to be trigger looping.

Trigger Looping Defined

There are multiple scenarios where trigger looping may occur, some which can be controlled and some which cannot. Below are a few examples:

  • If running Database.insert(new List<SObject1>{instance1,instance2,…},false) causes an exception from within SObject1 insert trigger context, SObject1 insert trigger re-fires, up to 3 times or until there is no more exception {cannot be controlled}
  • SObject2-after-trigger-fires, then a wokflow-rule-fires-and-updates-SObject2. This causes SObject2-after-trigger to re-fires {can be controlled however, there is no benefit in doing so}
  • Trigger1 spawns trigger2, which in turns spawns trigger3 … which in turns spawns trigger1 {can be controlled}

Control trigger looping with total control over trigger execution flow

There are several ways to avoid trigger looping including, but not limited to:

  • using a static variable to record first time entry in trigger and avoid re-entry
  • registration of trigger handler/helper methods either before or after method execution and avoiding method re-execution

These techniques would suffice on most small scale projects, and developers only need to look a step further when these techniques do not satisfy their needs.

The solution offered here relies on:

  1. Parent Trigger’s awareness of all direct child triggers spawned from within its own execution context.

Trigger execution context consists of a series of synchronous codes or actions invoked directly or indirectly from the start of the trigger all the way to the end.

This means when trigger A fires, executes and performs DML on objects B1 … Bn during execution, and triggers on B1 … Bn objects perform DML on object C11 … C1n, … ,Cn1 …,Cnn respectively, triggers on B1 … Bn are considered direct child triggers from Trigger A’s execution context.

  1. Parent Trigger deciding what direct child triggers should fire from within its own execution context, whenever applicable.

With this in mind, it follows that triggers should request permission from their parent triggers and proceed to execution if granted, or terminate if not.

Putting this in design yields the following diagram:

mini chartApexTriggerArchitecturalConcept_Diagram


—— A resulting implementation of the design is available for download here  ———

Benefits of the trigger pattern

-Avoiding unnecessary looping in trigger

-Total control over trigger execution flow

-Implemented effectively with knowledge of most/all of the business rules; which has benefits of its own, such as ease of maintenance of triggers.

About Clerval Fokle

Clerval Fokle is a Salesforce consultant at Statera. His background in math always get the best of him and draw him towards data analysis and optimization.

In his down time, Clerval can be found reading tech books/news or playing soccer.



Appleman, D. (2015). Triggers. In Advanced Apex Programming for and (3rd ed., p. 332). Desaware Publishing.