+ Start a Discussion
Sylvio AvillaSylvio Avilla 

List < AggregateResult> to visualpage

Hello!

I'm having some trouble trying to display information on visualpage from a List < AggregateResult>

Here the code
public class print {
   
    
  private ApexPages.StandardController controller {get; set;}
  public SGAAF__Itinerario_Diario__c itinerario {get;set;} 
  public List<SGAAF__Viagem__c> viagens {get;set;} 
  public List <AggregateResult> nbrOfRecords {get;set;}
  
   public print(ApexPages.StandardController controller) {
 
        this.controller = controller;
        itinerario = (SGAAF__Itinerario_Diario__c)controller.getRecord();
 
    viagens = [ Select ID, Name, From SGAAF__Viagem__c
    Where SGAAF__Viagem__c.SGAAF__Itinerario_Diario__r.ID = :itinerario.ID];

 List <AggregateResult> nbrOfRecords = [ SELECT  SGAAF__motorista__r.Id mot , COUNT(Name) cnt   FROM  SGAAF__Viagem__c Where SGAAF__motorista__r.Id IN : motoristaSet
  and SGAAF__Viagem__c.SGAAF__Itinerario_Diario__r.ID = :itinerario.ID GROUP by SGAAF__motorista__r.Id   ];

 
 
  }



and page
 
<apex:page standardController="SGAAF__Itinerario_Diario__c" extensions="print" sidebar="false" renderAs="pdf"
    applyBodyTag="false" >

<apex:variable value="{!nbrOfRecords }" var="nbr"/> 

 <apex:outputText value="{!nbr['cnt']}" />

......
Can someone help me?

thanks

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sylvio,

>> https://salesforce.stackexchange.com/questions/84767/display-an-aggregate-result-on-a-visualforce-page

As mentioned in the above link, The AggregateResult object is like a Map and so to access the values you need to use map syntax in the Visualforce:

<apex:column value="{!s['total']}"></apex:column>
PS

Here is a working example of using the map syntax:

<apex:page controller="MyController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!results}" var="r">
            <apex:column value="{!r['aaa']}"/>
            <apex:column value="{!r['bbb']}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

public with sharing class MyController {
    public AggregateResult[] results {
        get {
            return [select AccountId aaa, count(Id) bbb from Contact group by AccountId];
        }
    }
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Sylvio AvillaSylvio Avilla
hi ANUTEJ, thanks for your reply!

It didn't work for me!

I tried almost all topics that I could find on Google... this was one of them. It should be a simple solution, but as I'm not very good at coding, I cannot solve it!