Error: System.LimitException: Too many SOQL queries : 101
The following error appears when you exceed the Execution Governors Limit (you can run up to a total 100 SOQL queries in a single call or context).
System.LimitException: Too many SOQL queries: 101 error
Notes:
- All the SOQL queries in triggers fired from one call or context will be counted against the limit of 100.
- Salesforce cannot disable or raise the Governors Limit.
Resolution
Resolve the “Too many SOQL queries: 101” error
To fix the issue, change your code so that the number of SOQL fired is less than 100.
If you need to change the context, you can use @future annotation which will run the code asynchronously.
Best practices to avoid exceeding the Governors Limit
Since Apex runs on a multi-tenant platform, the Apex runtime engine strictly enforces limits to ensure code doesn’t monopolize shared resources.
- Avoid SOQL queries that are inside FOR loops.
- Follow the key coding principals for Apex Code in our Developer’s Guide.
- Review our best practices for Trigger and Bulk requests:
- Best practices for Triggers and Bulk requests (Force.com Apex Code Developer’s Guide)