function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sg88324sg88324 

Triggers' Governor limit on SOQL Queries

Hi All,

 

I know about "Too Many SOQL Queries:21" on triggers.

My question is , if there are around 6 triggers on an object, does the limit of 20 apply to the triggers of the object on the whole(i.e in above example, it wud be 20 limit for all the 6 triggers) or is it specific to each trigger(i.e for each trigger there wud be a limit of 20 SOQL queries)?

 

Please clarify.

 

Thanks,

Sandeep

JayantJayant

Hi Sandeep,

 

Actually the limit of 20 is on complete transaction that gets invoked from a trigger. It has nothing to do with number of triggers.

What I mean is, suppose I have a trigger on Contact that fires on After Insert, then once I insert a new Contact this trigger shall fire, now suppose this trigger has 2 SOQLs which get executed (suppose it has 5 SOQLs but only 2 execute because the code is conditional) and it calls a function in some class that has 3 SOQLs that fire, after this processing you update something on the parent Account, so appropriate Account triggers fire which have say 6 SOQLs that execute.

So now the flow and SOQLs is as below -

 

1. Contact trigger   ------------------------------>2 SOQLs

2. Function called by the above trigger--------------------------------------->3 SOQLs

3. Parent Account updated, before and after update account triggers fire --------------------------->6 SOQLs

 

so the number of fired SOQLs is 2+3+6 = 11.

 

So all the SOQLs that execute during a thread that started from a trigger irrespective of the object / method / class / trigger / Managed Packages would count   against your limit which is 20 in case of invocation from trigger.

JayantJayant

As a best practice, never write SOQL in loops and try to minimize the number of triggers on any object i.e. rather than having 3 after inserts, 2 before updates and 4 after updates on a single object, try to club them to just 1 After Insert, 1 Before Update and 1 After Update i.e. one trigger per event.

Also, if feasible try to make your triggers fire only if required for eg. suppose you may want to run some logic only when a field A changes on an object but the trigger might be firing even when A is untouched and some other field B might be updating which is not going to have any impact anywhere else in your application.

More triggers and SOQLs might not affect your work in short term but after a while they are really painful and a stage may be reached when merging is the only solution but the code to merge is so large that you may spend weeks on just merging your code.

sg88324sg88324

Thanks a lot Jayant.

Actually Our project has different consultants working on triggers. So there are more than one triggers on an object for each event. So the governor limit is exceeded. To confirm what u said, i posted the above question. Again, Thanks for clarifying.

 

Cheers,

Sandeep