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
Mateo AlcauterMateo Alcauter 

display an aggregate result on visualforce page

How can i display the results from this list on a visualforce page?

Controller:
public List<AggregateResult> BadgeList
    {
        get
        {
            List<AggregateResult> badges= [SELECT RecipientId, count(id) From WorkBadge GROUP BY RecipientId ORDER BY count(id) DESC LIMIT 5];                   
            return badges;
        }
        set;
    }

Visualforce page:
    <apex:pageBlock>
      <apex:pageBlockTable value="{!BadgeList}" var="badges" title="My Badges" columns="2" align="center" styleClass="table table-striped">
        <apex:facet name="header">My Badges</apex:facet>        
            <apex:column value="{!badges['RecipientId']}"/>
            <apex:column value="{!badges['count(id)']}"/>             
        </apex:pageBlockTable> 
        </apex:pageBlock>


Error message when i go to the visualforce page:
Unknown property 'AggregateResult.count(id)'
Error is in expression '{!badges['count(id)']}' in component <apex:column> in page companyhome
Error evaluating dynamic reference 'count(id)'
Best Answer chosen by Mateo Alcauter
Raj VakatiRaj Vakati

Use this code .

 
public List<AggregateResult> BadgeList
    {
        get
        {
            List<AggregateResult> badges= [SELECT RecipientId rec, count(id) cnt From WorkBadge GROUP BY RecipientId ORDER BY count(id) DESC LIMIT 5];                   
            return badges;
        }
        set;
    }

 
    <apex:pageBlock>
      <apex:pageBlockTable value="{!BadgeList}" var="badges" title="My Badges" columns="2" align="center" styleClass="table table-striped">
        <apex:facet name="header">My Badges</apex:facet>        
            <apex:column value="{!badges['cnt']}"/>
            <apex:column value="{!badges['rec']}"/>             
        </apex:pageBlockTable> 
        </apex:pageBlock>

 

All Answers

Raj VakatiRaj Vakati

Use this code .

 
public List<AggregateResult> BadgeList
    {
        get
        {
            List<AggregateResult> badges= [SELECT RecipientId rec, count(id) cnt From WorkBadge GROUP BY RecipientId ORDER BY count(id) DESC LIMIT 5];                   
            return badges;
        }
        set;
    }

 
    <apex:pageBlock>
      <apex:pageBlockTable value="{!BadgeList}" var="badges" title="My Badges" columns="2" align="center" styleClass="table table-striped">
        <apex:facet name="header">My Badges</apex:facet>        
            <apex:column value="{!badges['cnt']}"/>
            <apex:column value="{!badges['rec']}"/>             
        </apex:pageBlockTable> 
        </apex:pageBlock>

 
This was selected as the best answer
Mateo AlcauterMateo Alcauter
Thank you!!!
One more question. How do i get the name of the person? RecipientId.Name doesn't work. neither does RecipientId__r.Name
Raj VakatiRaj Vakati
Update the code as shown below.
public List<AggregateResult> BadgeList
    {
        get
        {
            List<AggregateResult> badges= [SELECT Recipient.Name rec, count(id) cnt From WorkBadge GROUP BY Recipient.Name ORDER BY count(id) DESC LIMIT 5];                   
            return badges;
        }
        set;
    }



 
Mateo AlcauterMateo Alcauter
Thanks!!!