+ Start a Discussion
prasanth kumarprasanth kumar 

simple aggregate program not working, please help

hi dudes, this is simple aggregate program and here i am sending the aggregate results (values) to the wrapper class, here i am getting error.  please some one help, thanks in advance.

This is the error showing in developer console.
Method does not exist or incorrect signature: [aggregateExample].get(String)              at line 11.

 
public class aggregateExample {
    public list<AggregateResult> result{set;get;}
    public list<ReportExample> reports{set;get;}
    public aggregateExample()
    {
        result=[select StageName,avg(amount)amt from opportunity group by StageName];
        reports=new list<ReportExample>();
        for(aggregateExample rs:result)
        {
            ReportExample re=new ReportExample();
            re.Stagen=(string)rs.get('stagename');
            re.amount=(decimal)rs.get('amt');
            reports.add(re);
            
        }
    }
    public class ReportExample
    {
        public string Stagen{set;get;}
        public decimal amount{set;get;}
    }
}

 
Best Answer chosen by prasanth kumar
Chandra Sekhar CH N VChandra Sekhar CH N V
Hi Prasanth,

The problem is with the way fields are retrieved and accessed in an Aggreaget Result set. You cannot iterate an sObject list directly using aggreaget list. I have changed it and let me know if it works:
 
public class aggregateExample{
    public list<AggregateResult> result{set;get;}
    public list<ReportExample> reports{set;get;}
    public aggregateExample()
    {
         reports=new list<ReportExample>();
        ReportExample re=new ReportExample();

//modified below code ...
        AggregateResult[] ar = [select StageName stn,avg(amount)amt from opportunity group by stagename];
        for(AggregateResult rs: ar)          //iterate aggregateResult instead of an sObject list
        {
            
            re.Stagen=(string)rs.get('stn');
            re.amount=(Decimal)rs.get('amt');
            reports.add(re);
            
        }
    }
    public class ReportExample
    {
        public string Stagen{set;get;}
        public decimal amount{set;get;}
    }
}