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.

1
  • 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'); Commented May 2, 2016 at 12:54

1 Answer 1

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'))
            ));
} 
6
  • Hi Keith i am not getting anything in chart...I want Product__r.Family values should appear in pie chart Commented May 2, 2016 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
    Commented May 2, 2016 at 13:06
  • If i run this query in workbench i am getting values like - Unknown_Field__1 f 26 Bedding Commented May 2, 2016 at 13:12
  • but its not appearing in vf page. How to check chart javascript error Commented May 2, 2016 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 login.salesforce.com/17181/logo180.png Failed to load resource: the server responded with a status of 404 (Not Found) Commented May 2, 2016 at 13:14

You must log in to answer this question.

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