+ Start a Discussion

Error in displaying AggregateResult data in visualforce page



I am querying a table ' expense reports ' for sum of  a field expenses using aggregate function  ,groupy by owner id of expense reports. I am using a wrapper class to display Owner's name and sum of its expenses. But when the code gets executed i get an error "System.SObjectException: Invalid field Owner for AggregateResult".


My Code:


//Definition / Constructor for Wrapper class
 public class wrapER
  public String ContractorName{get;set;}
  public Integer TotalAmount{get;set;}
  public wrapER( String con, Integer amt)
   ContractorName = con;
   TotalAmount = amt;

//Variable Declaration
 public List<wrapER> wrER = new List<wrapER>();

public wrapER[] getwrER()  
  wrER =  PETController();           
       return wrER;  
public List<wrapER>  PETController(){

List<aggregateResult> results_agg  = [select Owner.name,  SUM(Total_Amount_Expense_Item__c)  from Expense_Report__c where Project__c = :proj.id and Status__c = 'Approved'   Group by Owner.name];

for( integer i =0; i< results_agg.size();i++   )
  wrER.add(new wrapER(String.valueOf(results_agg[i].get('Owner')),Integer.valueOf( String.ValueOf(results_agg[i].get('expr0')))));
System.debug('**********res**' +results_agg);
return null;



Any idea  how can i fix this error.




Thnaks in advance



In my opinion you can use ownerid instead of owner.name .


Try the sample code given below :


List<aggregateResult> results_agg  = [select OwnerId,  SUM(Amount)  from Opportunity where id='00690000002umY3'  GROUP BY OwnerId];


system.debug('>>>>>>>'   + results_agg[0].get('OwnerId'));


Hope this helps.