Tuesday, 14 April 2020

Display Aggregate Result on Visualforce page

Hi ,

Here we are going to learn how to display aggregate result on visualforce page.

Here let's see how can we  display Opportunity count,sum of amount on Opportunities for each Stage (Propect,Closed Won,Closed Lost etc.,)

Let's have example:

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];        
    }
}

VFPage:

If you want to set headerValue for column while you are trying to display aggregate result you have to use "<apex:facet/> tag for setting headerValue.

Here you go for VF page example:

<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>


I gave demo on my channel "Salesforce Techbook" on the same here you can find out the link.


Reference:

No comments:

Post a Comment

Grounding Prompt Templates with Apex Merge Fields

 Hi, You can include an Apex merge field in a prompt template to surface data retrieved from a SOQL query or an external API. Apex is also u...