1

Trying to make a VF PieChart that displays a count of records "sold" by user over the last hour. I'm having trouble passing the correct values to display the Pie Chart. Below is the code for my controller. Any help is appreciated. Thanks.

public class SalesPerHourController {

    public datetime dt;
    public Integer counter;
    public List<AggregateResult> sales;

    public List<AggregateResult> getSales(){

        Datetime dt = System.now();
        dt = dt.addHours(-200);
        List<AggregateResult> sales = [SELECT Count(id) i, Sold_By__r.Name nm, Policy_Carrier__r.Name cr, Type
                            FROM Case
                            WHERE Sold_By_Date__c >:dt
                            GROUP BY Sold_By__r.Name, Policy_Carrier__r.Name, Type];              
        return sales;
    }

    public Integer getCounter(){

        List<AggregateResult> cnt = this.getSales();
        counter = cnt.size();

        return counter;
    }

    public List<PieWedgeData> getPieData(){

        List<PieWedgeData> data = new List<PieWedgeData>();
        List<AggregateResult> piesales = new List<AggregateResult>();                
        piesales = this.getSales();

        For(AggregateResult c:piesales){

            //This is where I am Struggling to input the correct values, I need the reps name as a String and then a record count. 
            data.add(new PieWedgeData(REPS NAME, COUNT OF RECORDS);
        }

        return data;
    }

    //Wrapper Class??
    public class PieWedgeData{

        public String name{get;set;}
        public Integer data{get;set;}

        public PieWedgeData(String name, Integer data){

            this.name = name;
            this.data = data;
        }
    }
}
1

The AggregateResult object has a map-like interface where you use a get method to get the data by the name nominated in the query. You then have to cast or otherwise convert to the correct type as the get method returns an Object:

public List<PieWedgeData> getPieData(){

    List<PieWedgeData> data = new List<PieWedgeData>();
    for (AggregateResult ar : getSales()) {
        data.add(new PieWedgeData(
                (String) ar.get('nm'),
                (Integer) ar.get('i')
                ));
    }
    return data;
}

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.