function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC 1904SFDC 1904 

Aggregate query has too many rows for direct assignment, use FOR loop. This exception is sometimes thrown when accessing a large set of child records (200 or more) of a retrieved sObject

We are getting this exception if number child records are more than 30.
What should we do? 
ANUTEJANUTEJ (Salesforce Developers) 
Hi,

I found the below link which states we get this error when we are trying to use an aggregate function (like COUNT) on a query that has too many rows.

Link: https://help.salesforce.com/articleView?id=000323566&language=en_US&type=1&mode=1

Kindly let me know if this helps and please close the thread by marking a best answer so that it would be useful for others in the future and also it would be helpful in keeping the community clean.

Regards,
Anutej
ayu sharma devayu sharma dev
Hello SFDC 1904 

I assume you are using a SOQL Query with a Nested query as result for a FOR loop. for e.g.
 
for( Account acct : [ SELECT ID, ( SELECT ID FROM Contacts ) FROM Account ] ){

   //Your code

}
If an Account has too many contacts it will cause the mentioned error.  

To resolve this error use below code:
 
List<Account> accountsList = [ SELECT ID, ( SELECT ID FROM Contacts ) FROM Account ];

for( Account acct : accountList ){

   //Your code

}

If problem still persists please let me. If it helped please mark this as the best answer.

Thanks and Regard
Ayush Sharma