Skip to content

Test failure: too many SOQL queries

Detection

  • String: Too many SOQL queries: 101
  • Applies to: Apex test failures

Examples

  • System.LimitException: Too many SOQL queries: 101

Resolution

The tested code runs more than 100 SOQL queries in one transaction, usually a query inside a loop, or a trigger that runs again and again.
- Move the queries out of the loops, and query once for all the records
- Stop the recursion of triggers with a static flag
- In the test, call the tested code between Test.startTest() and Test.stopTest() to get fresh limits

Comments