[SalesForce] CUMULATIVE_LIMIT_USAGE & CUMULATIVE_LIMIT_USAGE_END – Too many SOQL queries

We are getting 'Too many SOQL queries' on execution of Triggers on Account.
We are using lot of Managed packages.I am trying to debug a single test method to start with. I am looking at the debug logs, this is my approach:
1. Find out the first statement which says 'Number of SOQL queries: 52 out of 100 ******* CLOSE TO LIMIT , for example
2. Try to focus on the flow of the code between 'CODE_UNIT_STARTED' & 'CODE_UNIT_FINISHED' around the first statement found in step 1.
I could not find anything wrong with the queries so far, but is this the right approach?
I guess there is a limit usage log for the managed packages, which is giving the SOQL query usage for them, but can it so happen that because of the excessive queries fired in managed packages, the error is reported in the limit usage log of unmanaged classes?

Best Answer

I find that updates in for loops is a dead giveaway as the number at the conclusion of the previous code is low and then it hits the limit during one code execution of a class or trigger.

Recursion - either directly or indirectly (Trigger updates a related object which fires a different trigger and updates another related object which fires a trigger and updates the original object... and so on and so forth) can add up quickly by the time you get to five levels deep. You only need 20 soql queries to get caught out that way.

In our org we have found that the biggest problem we inherited off the original dev team was that triggers fire and run 2 or 3 queries but due to business logic don't actually do anything. The solution we used was to 'hide' the queries inside an if statement and check the business logic first. The better solution is to move all logic to helper classes and only call the helper when the business rules (as far as can be determined without soql) need it.

Related Topic