System.LimitException: Too many SOQL queries: meaning and how to fix it
Quick answer
System.LimitException: Too many SOQL queries: A transaction ran more SOQL queries than the per-transaction governor limit, usually a query inside a loop. Start with: Move queries out of loops: collect IDs first, then query once with IN :ids.
Typical message: System.LimitException: Too many SOQL queries: 101
Cause
A transaction ran more SOQL queries than the per-transaction governor limit, usually a query inside a loop.
How to fix System.LimitException: Too many SOQL queries
- Move queries out of loops: collect IDs first, then query once with IN :ids.
- Store results in a Map to look them up inside the loop.
- Check the debug log for the CUMULATIVE_LIMIT_USAGE section to see which code ran the queries.
Have a different message? Paste it into the error decoder, which matches it against 45 common Salesforce errors in your browser.
Frequently asked questions
What does System.LimitException: Too many SOQL queries mean in Salesforce?
A transaction ran more SOQL queries than the per-transaction governor limit, usually a query inside a loop.
How do I fix System.LimitException: Too many SOQL queries?
1. Move queries out of loops: collect IDs first, then query once with IN :ids. 2. Store results in a Map to look them up inside the loop. 3. Check the debug log for the CUMULATIVE_LIMIT_USAGE section to see which code ran the queries.
More Governor limits errors
SFDX Talk is unofficial documentation. It is not affiliated with, endorsed by, or sponsored by Salesforce, Inc. Message text varies slightly by release and context, so confirm against the official page before relying on it.