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
Kerry ProkselKerry Proksel 

Issue Displaying AggregateResults on VisualForce page

Hi Community,

I am trying to make a Visualforce table that will hold summed amounts. I want to have eight different SOQL queries that all use the SUM function, and then display them in a Visualforce table. 

Here's my controller so far:

public class PipelineController {
  
   public list<AggregateResult> newBizAmt = new list<AggregateResult>();
    public PipelineController(){
        newBizAmt = [select SUM(Amount) nba,RecordTypeForm__c 
                     from Opportunity 
                     WHERE RecordTypeForm__c = 'New Business' 
                     AND Current_Quarter__c = 'Q2-2016' 
                     AND isClosed = FALSE
                     GROUP BY RecordTypeForm__c];
    }
    public list<WrpClass> getResults(){
        list<WrpClass> nbaResults = new list <WrpClass>();
        for(AggregateResult ba:newBizAmt){
            WrpClass objwc = new WrpClass(ba);
            nbaResults.add(objwc);
        }
        return nbaResults;
    }
    class WrpClass{
        public Double Amount{get;set;}
        public WrpClass(AggregateResult ba){
            Amount=(Double)ba.get('nba');
        }
    }
}

I've been trying to display the result in a Visualforce page, but am having issues. Here's what I have:

<apex:page controller="PipelineController">
   <apex:pageBlock title="Sales Pipeline">
       <apex:pageBlockTable value ="{!results}" var="item">
        <apex:column value="{!nbaResults"/>
       </apex:pageBlockTable>
   </apex:pageBlock> 
</apex:page>


Could anyone help point me in the right direction? I know my column value is incorrect, but I'm not sure what to edit to make it work. Any help would be appreciated. 
Best Answer chosen by Kerry Proksel
Kerry ProkselKerry Proksel
Hey Raidan, I figured it out. The query was off (missing an underscore in between "New Business") but now it seems to work. Thank you!!

Here's the updated code (with an extra query):
public with sharing class PipelineController {
   
 public list<AggregateResult> newBizAmt = new list<AggregateResult>();
 public list<AggregateResult> newExpAmt = new list<AggregateResult>();   
    public PipelineController(){
        newBizAmt = [select SUM(Amount) nba, SUM(ExpectedRevenue) expRevenue, RecordType.Name 
                     from Opportunity 
                     WHERE RecordTypeForm__c = 'New_Business' 
                     AND Current_Quarter__c = 'Q2-2016' 
                     AND isClosed = FALSE
                     GROUP BY RecordType.Name];
    newExpAmt = [select SUM(ExpectedRevenue) expRevenue
                     from Opportunity 
                     WHERE RecordTypeForm__c = 'New_Business' 
                     AND Current_Quarter__c = 'Q2-2016' 
                     AND isClosed = FALSE ];    
    }
    
   public list<WrpClass> getResults(){
        list<WrpClass> nbaResults = new list <WrpClass>();
        for(AggregateResult ba:newBizAmt){
            WrpClass objwc = new WrpClass(ba);
            nbaResults.add(objwc);
        }
       
        return nbaResults;
       
    }
    class WrpClass{
        public Double Amount{get;set;}
        public Double AmountTwo{get;set;}
        public WrpClass(AggregateResult ba){
            Amount=(Double)ba.get('nba');
            AmountTwo=(Double)ba.get('expRevenue');
        }
    }
}

Here's the VF:
<apex:page controller="PipelineController">
   <apex:pageBlock title="Sales Pipeline">
       <apex:dataTable value ="{!results}" var="m">
        <apex:column value="{!m.Amount}"/>
           <apex:column value="{!m.AmountTwo}"/>
       </apex:dataTable>
   </apex:pageBlock> 
</apex:page>

All Answers

RaidanRaidan
Hi Kerry,

Try: <apex:column value="{!item.Amount}" /> to display the amount as a table column.
Kerry ProkselKerry Proksel
Thanks for the response Raidan. Unfortunately it compiles without errors, but doesn't display any the summed up amount. The Visualforce page is just the "Sales Pipeline" title. 
RaidanRaidan
Does your query actually return any records?
Kerry ProkselKerry Proksel
Hey Raidan, I figured it out. The query was off (missing an underscore in between "New Business") but now it seems to work. Thank you!!

Here's the updated code (with an extra query):
public with sharing class PipelineController {
   
 public list<AggregateResult> newBizAmt = new list<AggregateResult>();
 public list<AggregateResult> newExpAmt = new list<AggregateResult>();   
    public PipelineController(){
        newBizAmt = [select SUM(Amount) nba, SUM(ExpectedRevenue) expRevenue, RecordType.Name 
                     from Opportunity 
                     WHERE RecordTypeForm__c = 'New_Business' 
                     AND Current_Quarter__c = 'Q2-2016' 
                     AND isClosed = FALSE
                     GROUP BY RecordType.Name];
    newExpAmt = [select SUM(ExpectedRevenue) expRevenue
                     from Opportunity 
                     WHERE RecordTypeForm__c = 'New_Business' 
                     AND Current_Quarter__c = 'Q2-2016' 
                     AND isClosed = FALSE ];    
    }
    
   public list<WrpClass> getResults(){
        list<WrpClass> nbaResults = new list <WrpClass>();
        for(AggregateResult ba:newBizAmt){
            WrpClass objwc = new WrpClass(ba);
            nbaResults.add(objwc);
        }
       
        return nbaResults;
       
    }
    class WrpClass{
        public Double Amount{get;set;}
        public Double AmountTwo{get;set;}
        public WrpClass(AggregateResult ba){
            Amount=(Double)ba.get('nba');
            AmountTwo=(Double)ba.get('expRevenue');
        }
    }
}

Here's the VF:
<apex:page controller="PipelineController">
   <apex:pageBlock title="Sales Pipeline">
       <apex:dataTable value ="{!results}" var="m">
        <apex:column value="{!m.Amount}"/>
           <apex:column value="{!m.AmountTwo}"/>
       </apex:dataTable>
   </apex:pageBlock> 
</apex:page>
This was selected as the best answer
RaidanRaidan
Glad to hear that, Kerry! Please mark your question as solved.