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
Robert Wambold 10Robert Wambold 10 

Aggregate query has too many rows for direct assignment, use FOR loop. But I already have a FOR?

Hello All,

I inherited an Apex Class that's purpose is to check if Child Cases are still "Open" (not "Resolved" or "Closed") before allowing the Parent Case to be "Resolved" or "Closed".

The Apex Class has been working well until recently when a Case with 222 Child Cases and 59 Related Cases was trying to be "Closed".

The query of the Child Cases uses a FOR loop, but returned error:

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

I tried utilizing my companies Premier Support, but go nowhere. Lot's of links, but little help.

So I am asking if anyone has suggestions to make this Apex Class work.

Thanks friends.

Robert

 

Error:

System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop
External entry point

Problem Statement

//query all child cases

for(Case c:[SELECT Id, status,(select Id,status from cases) FROM  Case

 

Apex Class:

public class UpdateClosedCaseHelper {
    public static boolean skipSuperUserFilter = false;
    public static void updateCase(List<Case>  newcaselist,Map<Id,Case> oldMap,Map<Id,Case> newMap){
        if(!skipSuperUserFilter){
            //get current user info
            User u=[SELECT Id,Support_Super_User__c From User WHERE Id=:UserInfo.getUserId()];
            Set<Id> caseIdset=new Set<Id>();
            Map<Id,case> junctionMap=new Map<Id,case>();
            //get all case id's
            for(Case cd:newcaselist){
                caseIdset.add(cd.Id);
                junctionMap.put(cd.Id,cd);
            }
            //query all child cases                     
            for(Case c:[SELECT Id, status,(select Id,status from cases) FROM Case WHERE Id IN:caseIdset]){
                //check for value change
                if(oldMap.get(c.Id)!=newMap.get(c.Id)){
                    //case is not going to close
                    if(!newMap.get(c.Id).status.equals('Closed') 
                       &&
                       !newMap.get(c.Id).status.equals('Resolved')){
                           if(u.Support_Super_User__c){
                               //update case 
                           }
                           else{
                               if(oldMap.get(c.Id).status.equals('Closed'))
                                   newMap.get(c.Id).addError('Please contact your Almac Super User to re-open closed cases');
                               else{
                                   //update case 
                               }
                           }                        
                       }else{ 
                           //closing or resolving case
                           if((newMap.get(c.Id).status.equals('Closed') || newMap.get(c.Id).status.equals('Resolved')) && 
                              !isAllchildsClosed(c.cases)){                        
                                  if(u.Support_Super_User__c){
                                      //update case 
                                  }
                                  else{
                                      if(oldMap.get(c.Id).status.equals('Closed'))
                                          newMap.get(c.Id).addError('Please contact your Almac Super User to re-open closed cases');
                                      else{
                                          //update case 
                                      }
                                  }
                              }
                           else{
                               junctionMap.get(c.Id).addError('Child Case(s) need to be resolved before resolving a Parent Case');
                           }
                       }
                }
            }    
        }
    }
    public static boolean isAllchildsClosed(List<Case> childs){
        boolean flag=false;
        for(Case c:childs){
            if(!'Closed'.equals(c.Status) && !'Resolved'.equals(c.Status)){
                flag=true;
                break;
            }
        }
        return flag;
    }
}

 

Arvind_SinghArvind_Singh
Robert, 
This exception is thrown when accessing a large set of child records (200 or more) of a retrieved sObject inside the loop, or when getting the size of such a record set.

I would suggest to use for loop like below and see 
 
boolean flag=false;
    for(Case c:childs){
        if(!'Closed'.equals(c.Status) && !'Resolved'.equals(c.Status)){
            flag=true;
            break;
        }
    }
}
if (flag)){                        
    if(u.Support_Super_User__c){
        //update case 
    }