Too Many SOQL Queries Executions Error


Below is information on how to troubleshoot errors when hitting Salesforce's governor limits.

Error: Too many SOQL executions: 201.

Situation: Error occurs when closing an Account Period.


This error is due to certain code breaking Salesforce’s governor limits for Total Number of SOQL queries issued in an Apex transaction, which means you can run up to a total 200 SOQL queries in a single call or execution context. Whenever any of the governor limits are crossed, Salesforce will throw an error and halt the execution of code.

The error message tells us at exactly which line of certain code that we breached the governor limit. To fix the issue, you'll need to change your code in such a way that the number of SOQL fired is less than 200.

To debug the issue follow the below steps:

  1. In the client’s org, go to Setup and search for Debug Levels.

  2. Click New.

    • Name: SOQL Error
    • Set all Categories to INFO except >>> Apex
    • Profiling: Finest
    • Next to User Trace Flags click New.
    • Traced Entity Type: User
    • Traced Entity Name: Find the user that you logged into client’s org as.
    • Start Date: Current time
    • Expiration Date: 30 min from the current time
    • Debug Level type: SOQL Error
    • Click Save.
  3. Open and close an Accounting Period.

  4. Open a Debug Log that was just generated. If several Debug Logs were generated, pick the one that has the status Too many SOQL queries: 201.

  5. In the Debug Log search for the keyword Number of SOQL queries. You will see the number of SOQL queries that were cumulatively generated after each method.

    • The 1st grouping is the Number of SOQL queries generated by client’s code.
    • The 2nd grouping is the Number of SOQL queries generated by AS’s code.
    • Keep going down the Debug Log until you find:
      Number of SOQL queries: 201 out of 200 * CLOSE TO LIMIT
  6. Scroll up the debug log from that point to where you see something like:
    CODE_UNIT_STARTED|[EXTERNAL]|01q6F000000hKNT|DEL_XYZ on Billing trigger event AfterUpdate|__sfdc_trigger/DEL_XYZ
  7. Make note of the ‘Number of SOQL queries’ before that code started running. You will see a number like:
    Number of SOQL queries: 103 out of 200 * CLOSE TO LIMIT
    • This will show how many SOQL queries were generated by the method DEL_GSTLinesforBillings. In this case it is 98, before the code crashed
  8. Inform the client that the SOQL error is originating not from Accounting Seed’s package but from [insert the name of the class that you find step 7 above].

Further Reading

What is an asynchronous process?

What are governor limits?