Thursday, November 17, 2016

VisualForce Page to Display Number of Opportunities for an Account based on StageName Using Map in Pageblock Table



Page:

<apex:page StandardController="Account" extensions="OppCountInAccountController">
<apex:form >
<apex:pageBlock mode="edit">

<apex:pageBlockSection title="Account Details" >
<apex:outputText value="{!Account.name}"></apex:outputText>
</apex:pageBlockSection>

<apex:pageBlockSection title="Opportunity Details">
<apex:pageBlockTable value="{!listStrings}" var="stage"  >
<apex:column headerValue="Stage" value="{!Stage}" />
<apex:column headerValue="Count" value="{!omaps[stage]}" />
</apex:pageBlockTable> 

</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller:

public class OppCountInAccountController {
    
    public list<opportunity> listOpps {get;set;} 
    public set<String> Stageslist {get;set;} 
    Public list<Integer> Countlist {get;set;}
    Public List<String> listStrings {get;set;}
    Public Map<String,Integer> omaps {get;set;}
    
    public OppCountInAccountController(ApexPages.StandardController controller) {
        listOpps= [select id,Name,StageName,accountid from Opportunity where accountid=:apexpages.currentpage().getparameters().get('id')];
        Stageslist = new set<String>();
        for(Opportunity opp : listOpps){
            Stageslist.add(opp.stageName);
        }
        listStrings  = new List<String>(Stageslist);
        
        Countlist =new list<Integer>();
        omaps = new Map<String,Integer>();
        for(Integer i=0;i<listStrings.size();i++){
             Integer Num  = [select Count() from Opportunity where accountid=:apexpages.currentpage().getparameters().get('id') And stageName=:listStrings[i]];
             Countlist.add(Num);
             omaps.put(listStrings[i],Num);
         }
        
     }    
}

Note: This Page will work only if you pass an Account id to the page like this,
https://Test-Org-dev-ed--c.ap2.visual.force.com/apex/OppCountInAccount?id=00128000011VoTr

No comments:

Post a Comment