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
KeithWKeithW 

Apex Error "System.UnexpectedException: Field must be grouped or aggregated"

Could someone please help me with this error that is driving me crazy. I have Apex code that is schedule to run once a day, but errors due to my soql query with this message:

 

"caused by: System.UnexpectedException: Field must be grouped or aggregated: pkl_Year__c"

 

The code is:

 

 

global class ProgramSnapshot implements Database.Batchable<sObject>{
                    
    String query = 'Select SUM(num_3rdParty_Volume__c) num_sum3rd, SUM(num_Customer_Volume__c) num_sumCust, SUM(num_Our_Volume__c) num_sumOur, pkl_year__c FROM Program_Volume__c GROUP BY pkl_year__c LIMIT 100';    
    
    global database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        List<zzTest_DW__c> test_DWs = new List<zzTest_DW__c>();
    
        for(SObject s : scope){
            zzTest_DW__c a = new zzTest_DW__c();
            
            a.num_3rdParty_Volume__c = Integer.valueOf(String.valueOf(s.get('num_sum3rd')));
            a.num_Customer_Volume__c = Integer.valueOf(String.valueOf(s.get('num_sumCust')));
            a.num_Our_Volume__c = Integer.valueOf(String.valueOf(s.get('num_sumOur')));
            a.txt_GroupName__c = 'Test Group';
            a.txt_Model__c = 'Test Model';
            a.txt_ModelCode__c = 'Test Model Code';
            a.txt_Platform__c = 'Test Platform';
            a.txt_Program_Name__c = 'Test Program Name';
            a.txt_Year__c = String.valueOf(s.get('pkl_year__c'));
            a.date_Snapshot__c = Date.Today();
            
            test_DWs.add(a);
        }
        insert test_DWs;
    }
    
    global void finish(Database.BatchableContext BC){
        }
}

 

 

If I try to aggregate the pkl_year__c field, by doing a MAX(pkl_year__c), then I get an error of:

 

"System.QueryException: Grouped field should not be aggregated: pkl_Year__c"

 

What am I doing wrong??

Ankit AroraAnkit Arora

I don't see any problem with your code. I also tried the same thing on my org see below :

 

For e.g. If I write this then it will give me error : "Field must be grouped or aggregated: Id"

 

 

List<AggregateResult> ag = [select Id , name from Account GROUP BY Name] ;
System.debug(ag + ' ::::::::::::::') ;

 

Now if I write this :

 

 

List<AggregateResult> ag = [select COUNT(Id) , name from Account GROUP BY Name] ;
System.debug(ag + ' ::::::::::::::') ;

 

It resolves the error. Hope this may help you.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

 

deveshranjan.sinha1.3002556334deveshranjan.sinha1.3002556334

I am having query

 

select count(Id),AccountId from case where Case.IsClosed = false GROUP BY AccountId

 

but the Batchable class is throwing error:

 

04:11:54.319 (319016000)|FATAL_ERROR|System.UnexpectedException: Field must be grouped or aggregated: AccountId