Resolving Salesforce SOQL Query Governor Limits
Governor Limits on SOQL
Salesforce enforces limits such as 100 SOQL queries per synchronous transaction. Exceeding them throws System.LimitException: Too many SOQL queries.
How to Spot It
In the debug log, count SOQL_EXECUTE_BEGIN events. If you see the same query inside a loop, that's the cause.
Fixes
- Bulkify: move queries outside loops and use collections (
Map<Id, SObject>). - Use SOQL for loops with
WHERE Id IN :idSet. - Cache: store query results in a
Mapand reuse.
Reading the Log
The FormatHub Salesforce Log Cleaner aggregates SOQL execute events with row counts, making it easy to spot repeated queries and total rows consumed.