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
@login.ax974@login.ax974 

Error in batch apex

Hi,

 

Can someone please help me understand how to avoid the below error in the batch:

 

Aggregate query has too many rows for direct assignment, use FOR loop

 

The batch uses the below query:

 

SELECT Id, Type, Potential_Revenue__c, (SELECT Id, Total_Perm_Value__c, CreatedDate, Contract_Type__c, Status, Account_End_Date__c  from Agreements where status != \'Activated\') From Account';

 

And the error comes in at the below line of code:

 

global void execute(Database.BatchableContext BC, List<sObject> scope)
{
for(sobject s : scope)
{
Account a = (Account) s;
custIndicator = 0;
if(a.Agreements != null && a.Agreements.size() > 0) <--------- Error comes here.
{

 

thanks a lot!!

Best Answer chosen by Admin (Salesforce Developers) 
dmchengdmcheng

See this thread:

http://boards.developerforce.com/t5/Apex-Code-Development/Aggregate-query-has-too-many-rows-for-direct-assignment-use-for/td-p/309145

 

This limit does not appear to be documented anywhere.  You will probably have to omit the subquery from your initial batch query, then retrieve the Agreement records in the batch execute method. 

All Answers

Starz26Starz26

try:

 

global void execute(Database.BatchableContext BC, List<sObject> scope)
{
for(sobject s : scope)
{
Account a = (Account) s;
custIndicator = 0;

for(Agreements agr : a.agreements){

    

         //do stuff

   

}
{

@login.ax974@login.ax974

Hi,

 

 Thanks so much for the help. Really appreciate it but i am using the for loop as below but am still getting the error:

 

global void execute(Database.BatchableContext BC, List<sObject> scope)

for(sobject s : scope)
{
Account a = (Account) s;
custIndicator = 0;
if(a.Agreements != null && a.Agreements.size() > 0) <--------- Error comes here.
{

for(Integer i = 0; i < a.Agreements.size(); i++)

{
if(custIndicator <= 0)
{
if(a.Agreements[i].Agreements_Type__c == 'Value')
{
custIndicator++;
break;
}
}
}

Starz26Starz26

 

 

This Line: if(a.Agreements != null && a.Agreements.size() > 0) <--------- Error comes here.
{

 

is where you are having issues.

Which is why if you look at my example I removed it. For each record, I then loop through the children. That is where you do your processing. Try the example I provided...

 


@login.ax974@login.ax974

Hi,

 

Sure will do and respond back with the results.

 

Regards

dmchengdmcheng

See this thread:

http://boards.developerforce.com/t5/Apex-Code-Development/Aggregate-query-has-too-many-rows-for-direct-assignment-use-for/td-p/309145

 

This limit does not appear to be documented anywhere.  You will probably have to omit the subquery from your initial batch query, then retrieve the Agreement records in the batch execute method. 

This was selected as the best answer