+ Start a Discussion
Deepak Pandey 13Deepak Pandey 13 

opportunity stagename and thier record size

public class stagesize 
{
    public stagesize() 
    {
    lstOpp = new List<Opp>();
  stages = new list<string>();
        Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();
        System.debug(fieldResult);
        List<Schema.PicklistEntry> pichlstval = fieldResult.getPicklistValues(); 
        system.debug('--pichlstval -->>'+pichlstval );              
        for( Schema.PicklistEntry f : pichlstval )
        {
           stages.add(f.getLabel());
          System.debug('@@@@@stages' +stages);
        }
        
        map < string , list<opportunity>> lststage = new map < string , list<opportunity>>();
          for(Opportunity opp : [select id,Name,StageName from Opportunity])
          {
           if(lststage.get(opp.StageName)== null)
            {
             lststage.put(opp.StageName , new list<Opportunity>());
            }
           lststage.get(opp.StageName).add(opp);    
          System.debug('@@@@@lststage.size()' +lststage.size());
          }     
     for(string opp : stages)
     {
     integer i = lststage.size();
    String prefix = String.getCommonPrefix(stages);
     System.debug('@@@@@i' +i );
      System.debug('@@@@@prefix' +prefix );

    Opp o = new opp(prefix , i);
    lstOpp.add(o);
   //     lstOpp.add((prefix),(i));
     }
     
      }
     public List<Opp> lstOpp {get;set;} 
    public List<string> stages {get;set;}
    
    public class Opp  
    {
        public String stage {get;set;}    
        public Integer size {get; set;}
        
      public  opp(string s , Integer I)  
      {
        stage = s;
        size = i ;
      }
    }
}
Best Answer chosen by Deepak Pandey 13
Magesh Mani YadavMagesh Mani Yadav
Hi Deepak,

You don't have to write these many lines of code. As a best practice you code use the aggregate soql.
Here is the updated class and the Vf page.
 
public class stagesize 
{
    public List<AggregateResult> result {get;set;} 
    public stagesize() 
    {
       result = [select stagename,count(Id) total from Opportunity group by stagename];         
    }
}
 
<apex:page controller="stagesize">
    <apex:pageBlock >
        <apex:pageBlockSection>
        <apex:pageBlockTable value="{!result}" var = "res">
            <apex:column value="{!res['stagename']}">
                <apex:facet name="header">Stage Name</apex:facet>
            </apex:column>
            <apex:column value="{!res['total']}">
            	<apex:facet name="header">Count</apex:facet>
            </apex:column>
       	</apex:pageBlockTable>
        </apex:pageBlockSection>    	
    </apex:pageBlock>
</apex:page>
Hope your problem is solved