function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sai harishsai harish 

Invalid field for AggregateResult, Please help - Its urgent !!

Hello,

I am trying to use a wrapper class to show some values in a HTML table. I am trying to use Revenue and Revenue1 in the page. My page saves fine but when I try to preview, I am getting the following error, Invalid field rev for AggregateResult. When I try to use the Name field, it works fine.
Public Class SalesBudgetReport1{

Public List<RevenueForecast> RevenueForecasts {get; set;}

Public SalesBudgetReport1(){
    List<AggregateResult>  arList = new List<AggregateResult>(); 
    
List<AggregateResult> ar = [Select PM__c PM, sum(Revenue__c) rev from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 1 Group By  PM__c];
List<AggregateResult> ar1 = [Select PM__c PM, sum(Revenue__c) revenue1 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 2 Group By  PM__c];
List<AggregateResult> ar2 = [Select PM__c PM, sum(Revenue__c) revenue2 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 3 Group By  PM__c];
List<AggregateResult> ar3 = [Select PM__c PM, sum(Revenue__c) revenue3 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 4 Group By PM__c];
List<AggregateResult> ar4 = [Select PM__c PM, sum(Revenue__c) revenue4 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 5 Group By PM__c];
List<AggregateResult> ar5 = [Select PM__c PM, sum(Revenue__c) revenue5 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 6 Group By PM__c];
List<AggregateResult> ar6 = [Select PM__c PM, sum(Revenue__c) revenue6 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 7 Group By PM__c];
List<AggregateResult> ar7 = [Select PM__c PM, sum(Revenue__c) revenue7 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 8 Group By PM__c];
List<AggregateResult> ar8 = [Select PM__c PM, sum(Revenue__c) revenue8 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 9 Group By PM__c];
List<AggregateResult> ar9 = [Select PM__c PM, sum(Revenue__c) revenue9 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 10 Group By PM__c];
List<AggregateResult> ar10 = [Select PM__c PM, sum(Revenue__c) revenue10 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 11 Group By PM__c];
List<AggregateResult> ar11 = [Select PM__c PM, sum(Revenue__c) revenue11 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 12 Group By PM__c];
 
 arList.addAll(ar) ; 
 arList.addAll(ar1) ; 
 arList.addAll(ar2) ; 
 arList.addAll(ar3) ; 
 arList.addAll(ar4) ; 
 arList.addAll(ar5) ; 
 arList.addAll(ar6) ; 
 arList.addAll(ar7) ; 
 arList.addAll(ar8) ; 
 arList.addAll(ar9) ; 
 arList.addAll(ar10) ; 
 arList.addAll(ar11) ; 
  
 
RevenueForecasts = new List<RevenueForecast>();
for(AggregateResult arr : arList){
RevenueForecasts.add(new RevenueForecast(arr));
}
 
}
Public class RevenueForecast{
Public String Name {get; set;}
Public Decimal Revenue{get; set; }
Public Decimal Revenue1{get; set; }

Public RevenueForecast(AggregateResult arr){
Name = (String) arr.get('PM');
Revenue = (Decimal) arr.get('rev');
Revenue1 = (Decimal) arr.get('revenue1');


}
 
}
}

  Here is my class. Any help is appreciated.


 
Raj VakatiRaj Vakati
Share the code of the page ... i can fix it .. 
 
Raj VakatiRaj Vakati
Use this code and let me know if not working

 
Public Class SalesBudgetReport1{

Public List<RevenueForecast> RevenueForecasts {get; set;}

Public SalesBudgetReport1(){
    List<AggregateResult>  arList = new List<AggregateResult>(); 
    
List<AggregateResult> ar = [Select PM__c PM, sum(Revenue__c) rev from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 1 Group By  PM__c];
List<AggregateResult> ar1 = [Select PM__c PM, sum(Revenue__c) revenue1 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 2 Group By  PM__c];
List<AggregateResult> ar2 = [Select PM__c PM, sum(Revenue__c) revenue2 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 3 Group By  PM__c];
List<AggregateResult> ar3 = [Select PM__c PM, sum(Revenue__c) revenue3 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 4 Group By PM__c];
List<AggregateResult> ar4 = [Select PM__c PM, sum(Revenue__c) revenue4 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 5 Group By PM__c];
List<AggregateResult> ar5 = [Select PM__c PM, sum(Revenue__c) revenue5 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 6 Group By PM__c];
List<AggregateResult> ar6 = [Select PM__c PM, sum(Revenue__c) revenue6 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 7 Group By PM__c];
List<AggregateResult> ar7 = [Select PM__c PM, sum(Revenue__c) revenue7 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 8 Group By PM__c];
List<AggregateResult> ar8 = [Select PM__c PM, sum(Revenue__c) revenue8 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 9 Group By PM__c];
List<AggregateResult> ar9 = [Select PM__c PM, sum(Revenue__c) revenue9 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 10 Group By PM__c];
List<AggregateResult> ar10 = [Select PM__c PM, sum(Revenue__c) revenue10 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 11 Group By PM__c];
List<AggregateResult> ar11 = [Select PM__c PM, sum(Revenue__c) revenue11 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 12 Group By PM__c];
 
 arList.addAll(ar) ; 
 arList.addAll(ar1) ; 
 arList.addAll(ar2) ; 
 arList.addAll(ar3) ; 
 arList.addAll(ar4) ; 
 arList.addAll(ar5) ; 
 arList.addAll(ar6) ; 
 arList.addAll(ar7) ; 
 arList.addAll(ar8) ; 
 arList.addAll(ar9) ; 
 arList.addAll(ar10) ; 
 arList.addAll(ar11) ; 
  
 
RevenueForecasts = new List<RevenueForecast>();
for(AggregateResult arr : arList){
RevenueForecasts.add(new RevenueForecast(arr));
}
 
}
Public class RevenueForecast{
Public String Name {get; set;}
Public Decimal Revenue{get; set; }
Public Decimal Revenue1{get; set; }

Public RevenueForecast(AggregateResult arr){
Name = (String) arr.get('PM');
if(arr.get('rev')!=null){
Revenue = (Decimal) arr.get('rev');
}else{
	Revenue =0.0 ; 
}
if(arr.get('revenue1')!=null){
Revenue1 = (Decimal) arr.get('revenue1');
}else{
	Revenue1 =0.0 ;
}


}
 
}
}

 
sai harishsai harish
Hi Raj,

I am still getting the same error. Invalid field for AggregateResult. I can't understand what I am missing. Here is my code for the page. 
 
<apex:page controller="SalesBudgetReport1" showHeader="false">
<apex:form >
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>

<script>
$(document).ready( function () {
  var table = $('#example').DataTable()
  });
</script>
<style>
table{
border-collapse:collapse;
width: 100%;
}
th, td {
padding: 4px;
border: 1px solid black;
Padding-left: 8px;
Padding-right: 8px;
padding-top: 8px;
padding-bottom: 8px;
}
</style>

<apex:pageBlock >
<body>
<table id="example" class="display nowrap">
<thead>
<th>Project Manager</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</thead>


<apex:repeat value="{!RevenueForecasts}" var="a">
<tr>
<td>{!a['Name']}</td>
<td>{!a['Revenue']}</td>
<td> </td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</apex:repeat>
</table>
</body>
</apex:pageBlock>
</apex:form>
</apex:page>
Raj VakatiRaj Vakati
Can you try this 

 
<apex:page controller="SalesBudgetReport1" showHeader="false">
<apex:form >
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>

<script>
$(document).ready( function () {
  var table = $('#example').DataTable()
  });
</script>
<style>
table{
border-collapse:collapse;
width: 100%;
}
th, td {
padding: 4px;
border: 1px solid black;
Padding-left: 8px;
Padding-right: 8px;
padding-top: 8px;
padding-bottom: 8px;
}
</style>

<apex:pageBlock >
<body>
<table id="example" class="display nowrap">
<thead>
<th>Project Manager</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
</thead>


<apex:repeat value="{!RevenueForecasts}" var="a">
<tr>
<td>{!a.Name}</td>
<td>{!a.Revenue}</td>
<td> </td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</apex:repeat>
</table>
</body>
</apex:pageBlock>
</apex:form>
</apex:page>
 
Public Class SalesBudgetReport1{

Public List<RevenueForecast> RevenueForecasts {get; set;}

Public SalesBudgetReport1(){
    List<AggregateResult>  arList = new List<AggregateResult>(); 
    
List<AggregateResult> ar = [Select PM__c PM, sum(Revenue__c) rev from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 1 Group By  PM__c];
List<AggregateResult> ar1 = [Select PM__c PM, sum(Revenue__c) revenue1 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 2 Group By  PM__c];
List<AggregateResult> ar2 = [Select PM__c PM, sum(Revenue__c) revenue2 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 3 Group By  PM__c];
List<AggregateResult> ar3 = [Select PM__c PM, sum(Revenue__c) revenue3 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 4 Group By PM__c];
List<AggregateResult> ar4 = [Select PM__c PM, sum(Revenue__c) revenue4 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 5 Group By PM__c];
List<AggregateResult> ar5 = [Select PM__c PM, sum(Revenue__c) revenue5 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 6 Group By PM__c];
List<AggregateResult> ar6 = [Select PM__c PM, sum(Revenue__c) revenue6 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 7 Group By PM__c];
List<AggregateResult> ar7 = [Select PM__c PM, sum(Revenue__c) revenue7 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 8 Group By PM__c];
List<AggregateResult> ar8 = [Select PM__c PM, sum(Revenue__c) revenue8 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 9 Group By PM__c];
List<AggregateResult> ar9 = [Select PM__c PM, sum(Revenue__c) revenue9 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 10 Group By PM__c];
List<AggregateResult> ar10 = [Select PM__c PM, sum(Revenue__c) revenue10 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 11 Group By PM__c];
List<AggregateResult> ar11 = [Select PM__c PM, sum(Revenue__c) revenue11 from Revenue_Forecast__c WHERE PM__c!= null AND calendar_year(Forecast_Date__c ) = 2018 AND calendar_month(Forecast_Date__c ) = 12 Group By PM__c];
 
 arList.addAll(ar) ; 
 arList.addAll(ar1) ; 
 arList.addAll(ar2) ; 
 arList.addAll(ar3) ; 
 arList.addAll(ar4) ; 
 arList.addAll(ar5) ; 
 arList.addAll(ar6) ; 
 arList.addAll(ar7) ; 
 arList.addAll(ar8) ; 
 arList.addAll(ar9) ; 
 arList.addAll(ar10) ; 
 arList.addAll(ar11) ; 
  
 
RevenueForecasts = new List<RevenueForecast>();
for(AggregateResult arr : arList){
RevenueForecasts.add(new RevenueForecast(arr));
}
 
}
Public class RevenueForecast{
Public String Name {get; set;}
Public Decimal Revenue{get; set; }
Public Decimal Revenue1{get; set; }

Public RevenueForecast(AggregateResult arr){
Name = (String) arr.get('PM');
if(arr.get('rev')!=null){
Revenue = (Decimal) arr.get('rev');
}else{
	Revenue =0.0 ; 
}
if(arr.get('revenue1')!=null){
Revenue1 = (Decimal) arr.get('revenue1');
}else{
	Revenue1 =0.0 ;
}


}
 
}
}

 
sai harishsai harish
Hi Raj,

I am still getting the same error. 
Raj VakatiRaj Vakati
After using page and controller? please share the error message 
sai harishsai harish
Invalid field rev for AggregateResult. 
sai harishsai harish
Hi Raj,

I tried to debug everything. I am able to see individual values from ar to ar11.

Then I tried to debug arList. It has only values from ar, ar1, ar2, ar3.