0

I have a visualforce page that displays user contribution by CreatedById. Here is my controller code:

public List<AggregateResult> getTop6Contributors()
{
    return [
        SELECT CreatedById, CreatedBy.Name Name
        FROM Idea
        GROUP BY CreatedById, CreatedBy.Name
        ORDER BY count(CreatedById) desc limit 6
    ];
}

How do I display on my page the number of records a user has submitted?

Thanks!

  • Did you mean to alias CreatedBy.Name as Name or were you trying to query Name as well? – Adrian Larson Feb 2 '15 at 17:33
  • It was an alias. I don't have a Name field in my Idea sObject. – DevelopHawaii Feb 2 '15 at 18:12
2

I would lazy load that property since it probably will not change in the context of the page. Just add (and name) what you want to query.

public List<AggregateResult> top6Contributors
{
    get
    {
        if (top6Contributors == null)
        {
            top6Contributors = [
                SELECT CreatedById, CreatedBy.Name name,
                    count(Id) contributions
                FROM Idea
                GROUP BY CreatedById, CreatedBy.Name
                ORDER BY count(CreatedById) desc limit 6
            ];
        }
        return top6Contributors;
    }
    private set;
}

And on your page something like:

<apex:repeat value="{!top6Contributors}" value="contributor">
    <apex:outputText value="{!contributor.contributions}" />
</apex:repeat>
| improve this answer | |
  • I get the following error: "expecting a semi-colon, found '}' at line 26 column 4". This is where the curly bracket before "private set;" is. Any ideas? – DevelopHawaii Feb 2 '15 at 18:10
  • It was missing after the return in the getter. – Adrian Larson Feb 2 '15 at 18:15
  • Thanks. That worked. Now I am getting this error: "Grouped field should not be aggregated: CreatedById" after the SELECT command. – DevelopHawaii Feb 2 '15 at 18:28
  • Try passing Id to the count function instead. – Adrian Larson Feb 2 '15 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.