System.QueryException: Aggregate query does not support queryMore()

I am trying to deploy around 80 Components which include Apex Class,Trigger and Visualforce Pages. But it is failing due to below exception,

MyTrigger: execution of BeforeInsert caused by: System.QueryException: Aggregate query does not support queryMore(), use LIMIT to restrict the results to a single batch Trigg…

All test classes are run as a part of deployement process hence few of the test classes are making this trigger fire causing error. As its a change set deployment error, could not get the complete error statement. Also my trigger includes below code.

if(trigger.isBefore & trigger.isInsert) {
        for(AggregateResult ar : [select ABC__c, Name, max(Revision__c) maxRev from XYZ__c 
                                    where ABC__c in :oppMap.keySet()
                                    group by ABC__c, Name order by ABC__c, Name]) {
            decimal maxRev = Integer.valueOf(ar.get('maxRev'));
            string oppAppKey = String.valueOf(ar.get('ABC__c')) + String.valueOf(ar.get('Name'));
            lrMap.put(oppAppKey, maxRev);
        }
    }

Let me know how to fix this. Should i try using limit or is there any other solution?

Answer

I updated my code as below and it worked for me.

 Integer recordCount = [select count() from XYZ__c where ABC__c in :oppMap.keySet()];
            Integer sizeBlock = recordCount/2000 + (math.mod(recordCount,2000)!=0?1:0) ;
            Set<Id> setAppIds = new Set<Id>(); 

            //Iterate aggregate result with blocks of 2000 to avoid exception
            for(Integer i=0;i<sizeBlock;i++){            
                for(AggregateResult ar : [select id,ABC__c, Name, max(Revision__c) maxRev from XYZ__c
                                            where ABC__c in :oppMap.keySet() and id not in :setAppIds 
                                            group by id,ABC__c, Name order by ABC__c, Name limit 2000]) {
                    decimal maxRev = Integer.valueOf(ar.get('maxRev'));
                    string oppAppKey = String.valueOf(ar.get('ABC__c')) + String.valueOf(ar.get('Name'));
                    lrMap.put(oppAppKey, maxRev);
                    setAppIds.add(Id.valueOf(String.valueOf(ar.get('id'))));
                }
            }

courtesy :- @Peter Knolle

Attribution
Source : Link , Question Author : snehakem , Answer Author : snehakem

Leave a Comment