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
JoshTonksJoshTonks 

Invalid field field_name__c for AggregateResult

Hi,

I need some guidance on to what im not doing correctly.

Im trying to build a custom controller which gets the number of 3 fields on records that are due to be shipped in the next few days grouped by date. 
 
public class LogisticsQuantity {
//your main class
//
    Date STARTDAY = Date.today();
    Date ENDDAY = STARTDAY.addDays(5);

    public Summary[] Summaries { get; set; }

    public LogisticsQuantity() {
        AggregateResult[] results = [
            SELECT Last_Date_for_Dispatch__c, Count(Id),SUM(Hard_Wired_Dash_Camera__c), SUM(New_Unit_Qty__c), SUM(Service_Unit_Qty__c) FROM Unit_Request__c WHERE Last_Date_for_Dispatch__c >= :STARTDAY AND Last_Date_for_Dispatch__c < :ENDDAY 
            GROUP BY Last_Date_for_Dispatch__c 
        ];
        Summaries = new List<Summary>();
        for (AggregateResult ar : results) {
            Summaries.add(new Summary(ar));
        }
    }

    // wrapper class to hold aggregate data
    public class Summary {
        public Integer NewQuantity { get; set; }
        public Integer SvcQuantity { get; set; }
        public Integer CamQuantity { get; set; }
        public String Name { get; set; }

        public Summary(AggregateResult ar) {
            CamQuantity = (Integer) ar.get('Hard_Wired_Dash_Camera__c');
            NewQuantity = (Integer) ar.get('New_Unit_Qty__c');
            SvcQuantity = (Integer) ar.get('Service_Unit_Qty__c');
            Name = (String) ar.get('Last_Date_for_Dispatch__c');
        }
    }

}
I created a visualforce page to test it out on.
<apex:page controller="LogisticsQuantity">
    <apex:form >
        <apex:repeat value="{!Summaries}" var="summary">
            {!summary.Name}: {!summary.NewQuantity} {!summary.SvcQuantity} {!summary.CamQuantity}<br/>
        </apex:repeat>
    </apex:form>
    

</apex:page>
Its blank when there isnt any records that meet any of the criteria but i get Invalid field field_name__c for AggregateResult. Its happening for all 3 fields that im trying to get SUM of.

All these fields are number formula fields with no decimal points. And display as 0 as default value if there is no number in fields it pulls from. Ive never done Aggregate results.


 
Best Answer chosen by JoshTonks
a sangeetha 5a sangeetha 5
Hi,
Please find below the class and page..I tried in dev org and getting results... hope it helps 

Apex Class:
public class LogisticsQuantity {
    Date STARTDAY = Date.today();
    Date ENDDAY = STARTDAY.addDays(5);
    public Summary[] Summaries { get; set; }
    public LogisticsQuantity() {
        AggregateResult[] results = [
            SELECT tryagain__Last_Date_for_Dispatch__c, Count(Id),SUM(tryagain__Hard_Wired_Dash_Camera__c) hard, 
            SUM(tryagain__New_Unit_Qty__c) newUnit , 
            SUM(tryagain__Service_Unit_Qty__c) serv FROM tryagain__Test_Object__c 
            WHERE tryagain__Last_Date_for_Dispatch__c >= today group by tryagain__Last_Date_for_Dispatch__c];
        Summaries = new List<Summary>();
        for (AggregateResult ar : results) {
            Summaries.add(new Summary(ar));
        }
    }
// wrapper class to hold aggregate data
    public class Summary {
        public decimal NewQuantity { get; set; }
        public decimal SvcQuantity { get; set; }
        public decimal CamQuantity { get; set; }
        public date Name { get; set; }
        public Summary(AggregateResult ar) {
            CamQuantity = (decimal) ar.get('hard');
            NewQuantity = (decimal) ar.get('newUnit');
            SvcQuantity = (decimal) ar.get('serv');
            Name = (date) ar.get('tryagain__Last_Date_for_Dispatch__c');
        }
    }
}

VF page:
<apex:page controller="LogisticsQuantity" showHeader="false">
    <apex:form >
        <apex:repeat value="{!Summaries}" var="summary">
            {!summary.Name}: <br/>{!summary.NewQuantity}<br/>{!summary.SvcQuantity}<br/> {!summary.CamQuantity}<hr/>
        </apex:repeat>
    </apex:form>
    

</apex:page>

Results:

User-added image
Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue.

Thanks,
Sangeetha A