A SOQL query throws the exception "Aggregate query has too many rows for direct assignment" in two different scenarios:
When a subselect in a SOQL aggregate query returns more than 200 rows, Salesforce raises this exception when you attempt to assign the results directly to a variable. To resolve this, iterate over the results using a FOR loop instead of direct assignment.
If all subselects are returning fewer than 200 rows but the exception still occurs, the issue may be that the SOQL query is selecting Long Text Area or Rich Text Area fields.
These field types can store large amounts of data. Salesforce applies additional memory constraints to queries that reference many large object (LOB) fields to prevent excessive memory consumption. This constraint can reduce the effective row limit of the subselect below 200, causing the exception to be raised even when the actual row count appears low.
To resolve this exception, reduce the memory footprint of your SOQL query:
If your subselect genuinely exceeds 200 rows (Scenario 1), refactor the query to use a FOR loop to iterate over the results rather than assigning them directly to a List variable.
000381135

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.