How to read field values from AggregateResult?

476    Asked by dipesh_9001 in Salesforce , Asked on May 9, 2023

I need to use group by cube to display the count of added services for each group for each package. I get an error on line 18: method does not exist or incorrect signature: void get(String) from the type List. How do I correct this?

public class AddedServicesGrouping {
                                   FROM Inspection_Services__r)
                                   FROM Inspection__c
                                   WHERE Status__c = 'Completed' AND Month_Since_Launch__c > 0];
    public list getservices(){
        for(Inspection__c i: inspections){
            for(Inspection_Service__c addedservices : i.Inspection_Services__r){
                AggregateResult[] ar =
                    [SELECT Added_Services_Group__c AddedService, count_distinct(id) cnt, Inspection_Package__c Package,
                    GROUPING(Added_Services_Group__c), GROUPING(Inspection_Package__c) 
                    FROM Inspection_Service__c
                    GROUP BY CUBE (Added_Services_Group__c, Inspection_Package__c)];
                integer n = integer.valueof(ar.get('cnt'));
            }
        } 
        return inspections;
    }
}
    public list inspections = [SELECT Id, (SELECT Added_Services_Group__c, Inspection_Package__c, Id
Answered by David Edmunds
You need to call get from AggregateResult, not List.

Instead of ar.get('cnt'), you could for instance do ar[0].get('cnt').


Your Answer

Interviews

Parent Categories