Displaying Aggregate result on Visualforce using SOQL | Techila
1078
single,single-post,postid-1078,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Displaying Aggregate result on Visualforce using SOQL

aggr

Displaying Aggregate result on Visualforce using SOQL

Aggregate functions give a very beautiful meaning to data altogether.

 

For those of you who knew SQL you obviously know what aggregate functions are and how they beautify your code.

Those who do not know, aggregate functions help you aggregate the date based on certain field.

For example, you want to show the count and Opportunities created on a day, you can aggregate them using SOQL.


AggregateResult[] groupedResults  = [SELECT Name, Count(Amount) FROM Opportunity GROUP BY CreatedDate];


So how do we display it on Visualforce:

Just iterate on the AggregateResults and get elements one by one.


for (AggregateResult ar : groupedResults)  {

    System.debug(‘Created Date’ + ar.get(‘CreatedDate’));

}



A few pointers for debugging:

1. Always use API version 18.0+ while using Aggregate Functions. If you are eclipse, you might want to update it. And frankly if you are still using older version of eclipse, my mind wants to scream and ask you…why?

2. If you are Aggregating using a relationship e.g., Object1__r.object2__c in your SOQL, while fetching from AggregateResults you only need Object2__c, ar.get(‘Object2__c’) I am not really sure why, because I found no documentation supporting this, but it worked for me in the code.


 

Courtesyhttp://www.sforce.ninja/2010/11/displaying-aggregate-result-on.html

Author: techila

No Comments

Post A Comment