2

I am trying to create an apex chart. While running i am getting the following error -

Invalid field Product__r.Family for AggregateResult

public list<wrapperclass> getPieData() {
 List<wrapperclass> data = new List<wrapperclass>();

List<Work_Order_Item__c> memb = new List<Work_Order_Item__c>();             


 AggregateResult[] groupedResults  = [SELECT count(Id),Product__r.Family FROM Work_Order_Item__c group by Product__r.Family];

 for (AggregateResult ar : groupedResults)  {            
                    if(ar.get('Product__r.Family')!=null)
                    {
                     data.add(new wrapperclass(
                         String.ValueOf(ar.get('Product__r.Family')),
                         Double.ValueOf(ar.get('expr0'))
                         ));
                    }    
             }      
             return data;

}

Vf Page -

<apex:chart height="350" width="450" data="{!pieData}">
            <apex:pieSeries dataField="data" labelField="Name"/>
            <apex:legend position="bottom"/>
        </apex:chart>

Error Javascript -

SfdcCore.js:384 Visualforce Chart: Error loading configuration for chart 'jid0jid1pbjid3jid4': Unable to infer data model from result: result is not an Arrayb.resolve.b.provide.log @ SfdcCore.js:384
https://login.salesforce.com/17181/logo180.png Failed to load resource: the server responded with a status of 404 (Not Found)

Can anyone tell what i am doing wrong.

Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
sfdc_to_learn
  • 1,732
  • 2
  • 32
  • 89
  • You should use a field alias in the aggregate query (something like SELECT count(Id) cnt, Product__r.Family prodFam FROM Work_Order_Time....) and then use that alias to get the values, so ar.get('cnt'); and ar.get('prodFam'); – user254875486 May 02 '16 at 12:54

1 Answers1

1

The aggregate case is bit different to normal SOQL so I think this is what is needed:

AggregateResult[] groupedResults  = [
        SELECT count(Id) c, Product__r.Family f
        FROM Work_Order_Item__c
        WHERE Product__r.Family != null
        GROUP BY Product__r.Family
        ];
for (AggregateResult ar : groupedResults)  {            
    data.add(new wrapperclass(
            String.ValueOf(ar.get('f')),
            Double.ValueOf(ar.get('c'))
            ));
} 
Keith C
  • 135,775
  • 26
  • 201
  • 437
  • Hi Keith i am not getting anything in chart...I want Product__r.Family values should appear in pie chart – sfdc_to_learn May 02 '16 at 13:01
  • First thing to do is output {!PieData.size} into the page to make sure the list has values. After that, check the browser's JavaScript console for error from the chart JavaScript. – Keith C May 02 '16 at 13:06
  • If i run this query in workbench i am getting values like - Unknown_Field__1 f 26 Bedding – sfdc_to_learn May 02 '16 at 13:12
  • but its not appearing in vf page. How to check chart javascript error – sfdc_to_learn May 02 '16 at 13:13
  • Hi Keith. I m getting this error when i do inspect element -SfdcCore.js:384 Visualforce Chart: Error loading configuration for chart 'jid0jid1pbjid3jid4': Unable to infer data model from result: result is not an Arrayb.resolve.b.provide.log @ SfdcCore.js:384 https://login.salesforce.com/17181/logo180.png Failed to load resource: the server responded with a status of 404 (Not Found) – sfdc_to_learn May 02 '16 at 13:14
  • @Sid See How do I start to debug my own Visualforce/JavaScript?. You'll have to figure this out yourself: first check whether the data created in the controller is OK or not and if it is go on to debug the client-side. – Keith C May 02 '16 at 13:15