System.QueryException in Apex SOQL query row limit
0 reputation · 26 Jul 2025, 13:52 UTC
0 reputation · 26 Jul 2025, 13:52 UTC
Apex transactions enforce a per‑execution SOQL query limit of 100 queries (200 for Developer and Performance editions), and exceeding this threshold triggers a System.QueryException. One unresolved behavior involves whether query row limit enforcement (governor limit of 50,000 rows per query, with cumulative row counts affecting transaction limits) consistently respects these thresholds when Apex code includes complex relationship queries or nested collections. Variations in edition‑specific governor limit calculations and undocumented optimization heuristics may cause intermittent limit breaches that pass test‑class validation but fail in production data volumes with larger heap footprints.
30775 reputation · 26 Jul 2025, 18:09 UTC
The System.QueryException you see is thrown when the Apex transaction exceeds the governor limit for the number of SOQL queries, not when the row limit is exceeded.
In a test class the data set is small, so the cumulative row count stays well below 50 000 and the query count stays below 100. In production (or a full‑copy sandbox loaded with realistic data) the same logic may iterate over many records, causing the query count to creep past 100 or the row total to surpass 50 000, triggering the exception.
Integer queriesUsed = Limits.getQueries();
Integer queryLimit = Limits.getLimitQueries();
Integer rowsUsed = Limits.getQueryRows();
Integer rowLimit = Limits.getLimitQueryRows();
System.debug('Queries: '+queriesUsed+'/'+queryLimit);
System.debug('Rows: '+rowsUsed+'/'+rowLimit);
Add these debug statements around the suspect code path and review the log in a full‑copy sandbox that contains production‑scale data.
To confirm whether the exception is due to the query‑count limit or the row‑count limit, please share the debug log excerpt that shows the Number of SOQL queries and Number of query rows values at the point of failure. Knowing which counter is exceeded will tell you whether to focus on reducing query calls or on lowering the cumulative row volume.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.