5

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 !

  • 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 '15 at 14:06
  • @Eric the syntax does not work, but how can I make a wrapper class to solve my problemplease ? – dou Jul 24 '15 at 14:13
4

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

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.