0

I have a class that is correctly pulling an AggregateResult in from a child object on a custom object. I'd like to use an pex:pageBlockTable to display the aggregateResults but when trying to setup the header I get an error.

This works per system debug checks:

    public List<AggregateResult> getAggregateList(){
            Parent_Custom_object__c TheCS = (Parent_Custom_object__c)sc.getRecord();
            List<AggregateResult> AggResults = new List <AggregateResult>();

         AggregateResult[] aggQuery = [select
     zone__c zone,
      SUM(Added_Value__c)TotalAddedValue,
      SUM(delivered_units__c)TotalDelivered
      from Custom_Child_object__c 
      where Parent_of_Custom_Child_object__c =:TheCS.id
      GROUP BY zone__c    
             ]; 

     if(aggQuery!=null){
         for(AggregateResult QAR:aggQuery)
      {
        AggResults.add(QAR); 
    }

Visualforce page:

<apex:pageBlock title="AggregateList">   

     <apex:pageBlockTable value="{!AggregateList}" var="d" id="Zone" title="Aggregate"> 
     <apex:column value="{!d.Zone}" headervalue="Zone" ></apex:column>
 </apex:pageBlockTable> 

Error: Error: Invalid field Zone for SObject AggregateResult

2

You can only use the dot-notation for "concrete" fields (fields that can be verified through metadata). For example, {!Account.Name} is valid, because the system can verify that Account.Name is a real field. However, AggregateResult is a dynamic object. As such, you need to use the "dynamic" syntax:

<apex:column value="{!d['Zone']}" headerValue="Zone" />

The [field] notation allows you to use a string variable to dynamically access a field that may or may not exist on an sObject or AggregateResult variable.

| improve this answer | |
  • Hmm I am still getting the same error. – jaw999 Nov 11 '19 at 20:33
  • @jaw999 I believe it may be case-sensitive? Make sure the alias and the "field name" match in case. – sfdcfox Nov 11 '19 at 20:36
  • I have matched case and it still shows the same error, <apex:column value="{!d['zone']}" headerValue="zone" /> produces Error: Invalid field zone for SObject AggregateResult – jaw999 Nov 11 '19 at 20:56
  • I tried using the facet idea per this post but I didn't render alokagrawal.com/blog/archives/74 – jaw999 Nov 12 '19 at 14:21
  • OK, not sure what my error was but this is working now, thanks! @sfdcfox – jaw999 Nov 13 '19 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.