+ Start a Discussion
doudou 

Display a soql query in visualforce page

Hello Salesforce !
I need your help for displaying an aggregate result from soql query on my visualforce page
Here is apart of my controller :
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 for the 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>
When i try to save my visualforce page, it gives me an error :  Invalid field total for SObject AggregateResult
I'm a beginer in salesforce and I don't know how to fix that error and display the result of my query in my page...

Thank you :)
Krishna SubramanyamKrishna Subramanyam
Hi elicec,
  there is no problem in apex code. added  below few changes to your code.

Controller code:
step1: comment line8 in controller code and add this line  somme.add(new Summary(ar));
step2: u need to create a wrapper class to hold the aggregate data as inner class.
if you make these two changes to your code . it will work properly.

    public class Summary {
        public integer total{ get; private set; }
       
        public Summary(AggregateResult ar) {
            total = (integer) ar.get('total');
           }
    }


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'); // remove this and
somme.add(new Summary(ar));  //add this line
}

}


// wrapper class to hold aggregate data as inner class
    public class Summary {
        public integer total{ get; private set; }
       
        public Summary(AggregateResult ar) {
            total = (integer) ar.get('total');
           }
    }
doudou
Thank you Krishna Subramanyam, 
I tried what you said, but when it come to the line 
somme.add(new Summary(ar));

it gives me an error : Incompatible element type Controller2.Summary for collection of AggregateResult ...
Krishna SubramanyamKrishna Subramanyam
hi
i forgot to change 
public List<AggregateResult> somme {get; set;}
 line to 
public AggregateResult[] somme {get;set;}
try this