6

I try to display on my visualforce page the result of a soql query (an aggregate result precisely).

Here is my controller (a partof it) :

public List<AggregateResult> somme {get; set;}
public Integer total {get; set;}

public Controller2(){
somme = [select SUM(Montant_total_calcule__c)total from Commande__c where Id IN :sCommandeId];

for(AggregateResult ar : somme){
total = (Integer) ar.get('total');
}

}

And the part of my page :

<apex:form>
<apex:pageBlockTable value="{!somme}" var="s">
<apex:column>Montant total : </apex:column>
<apex:column value="{!s.total}"></apex:column>
</apex:pageBlockTable>
</apex:form>

If anyone can help me, I got an error on my visualforce page : Invalid field total for SObject AggregateResult I don't know how to fix it... And how to make my code working.

Thanks !

2
  • IN order to get Aggregate results you need to access the subject map, so in the vf page (not sure if it will work) did you try {!s['total']} If that does not work, you may need to consider a wrapper class to handle the display of the results
    – Eric
    Jul 24, 2015 at 14:06
  • @Eric the syntax does not work, but how can I make a wrapper class to solve my problemplease ?
    – dou
    Jul 24, 2015 at 14:13

3 Answers 3

5

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];
        }
    }
}
2
  • I just try this syntax, but it still dosen't works and give me the error...
    – dou
    Jul 24, 2015 at 14:11
  • @dou See the PS. Map syntax does work.
    – Keith C
    Jul 24, 2015 at 15:08
1

You can write a wrapper class and map your result into the wrapper class list. And you can use wrapper class to display your data. Something like below code should work.

//your main class

Public List<wrapperClass> wpList{get; set;}
//controller
wpList = new List<wrapperClass>();

for(AggregateResult ar : somme){
    wrapperClass wp = new wrapperClass(ar);
    wpList.add(wp);
}

public class wrapperClass{
    public Integer total {get; set;}

    public wrapperClass(AggregateResult ar){
        total = (Integer) ar.get('total');
    }
}

//vf page

<apex:form>
<apex:pageBlockTable value="{!wpList}" var="s">
<apex:column>Montant total : </apex:column>
<apex:column value="{!s.total}"></apex:column>
</apex:pageBlockTable>
</apex:form>
1
  • While this answer does indeed work, it is a lot more work than what is required and increases the complexity. KeithC's answer is actually the correct answer.
    – Eric
    Jul 24, 2015 at 15:17
0

If you want to use "headerValue" in column with Aggregate Result then you can't use it directly. You have to use <apex:facet> for setting header value.

here is working code:

Apex Class:

public with sharing class AggregateResultController {
    public List<AggregateResult> aggregateResultList{get;set;}
    public List<AggregateResult> campaignResultList{get;set;}
    public AggregateResultController() {
        aggregateResultList = new List<AggregateResult>();
        aggregateResultList = [select count(id) oppCount,sum(Amount) oppSum,StageName oppStage 
                            from Opportunity group by StageName];        
    }
}

VF Page:

<apex:page controller="AggregateResultController">

    <h1>Aggregate Resul Example</h1>
    <apex:pageBlock title="Opportunity Count with Stage Name">
        <apex:pageBlockTable value="{!aggregateResultList}" var="agResultObj">
            <apex:column value="{!agResultObj['oppCount']}">
                <apex:facet name="header">Count</apex:facet>
            </apex:column>
            <apex:column value="{!agResultObj['oppSum']}">
                <apex:facet name="header">Sum</apex:facet>
            </apex:column>
            <apex:column value="{!agResultObj['oppStage']}">
                <apex:facet name="header">Stage Name</apex:facet>
            </apex:column>
        </apex:pageBlockTable>       
    </apex:pageBlock>    
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .