0

I want to count records based on different values provided to records using Status__c field. Status__c is a picklist field. For efficiency purpose, I want to run one query but get different result of counts of records based on Status__c field. So, I am using Query with GROUP BY. However, getting this error

System.SObjectException: Invalid field Status__c for AggregateResult

Code

public static List<AggregateResult> getEverythingUsingGroubBy(){
    List<AggregateResult> lstContact = new List<AggregateResult>();
    lstContact = [SELECT count(Id) recordCount FROM Partner_Task__c GROUP BY Status__c];
    for (AggregateResult ar : lstContact) {
        System.debug('Count of tasks are ' + ar.get('Status__c') +'is '+ ar.get('recordCount'));
    }
    return lstContact;
}

1 Answer 1

0

The problem here is you are using Status__c without fetching it from query.

change

 lstContact = [SELECT count(Id) recordCount FROM Partner_Task__c GROUP BY Status__c];

To:

 lstContact = [SELECT count(Id) recordCount,Status__c FROM Partner_Task__c GROUP BY Status__c];
1
  • 1
    thanks, it resolved my issue.
    – SF Learner
    Jul 11, 2023 at 8:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .