Loading

Aggregate query has too many rows for direct assignment but less than 200 rows

Date de publication: May 15, 2026
Description

A SOQL query throws the exception "Aggregate query has too many rows for direct assignment" in two different scenarios:

Scenario 1: Subselect Returns More Than 200 Rows

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.

Scenario 2: Subselect Returns Fewer Than 200 Rows — Long Text or Rich Text Fields

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.

Résolution

To resolve this exception, reduce the memory footprint of your SOQL query:

  • Remove unnecessary Long Text Area and Rich Text Area fields from your SOQL query. Include only the fields your code actually needs.
  • Decrease the field length of Long Text Area or Rich Text Area fields if the full length is not required.

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.

Ressources supplémentaires

SOQL FOR Loop — Apex Developer Guide

Numéro d’article de la base de connaissances

000381135

 
Chargement
Salesforce Help | Article