Tuesday, August 19, 2014

Displaying Records in Visualforce page in monthly wise?

Displaying Records in Vfpage in monthly wise?




Hi,
In this post i am giving an Example of how to display the opportunities that are created by month wise in salesforce.

Controller Class:

public class OpportunitiesByMonthwiseCLS {

 public List <OpportunityInfo> opportunityList{get;set;}
    //Constructor
    public OpportunitiesByMonthwiseCLS(ApexPages.StandardController controller) 
    {
        opportunityList = new list <OpportunityInfo>();
        for(integer i=0;i<System.now().month();i++)
        {
            OpportunityInfo oinfo = new OpportunityInfo();
            oinfo.month = datetime.newinstance(2013,1,1).addmonths(i).format('MMM');
            oinfo.count = 0;
            opportunityList.add(oinfo );
        }
        AggregateResult[] groupedResults  = [SELECT Count(id)monthCount,CALENDAR_MONTH(createdDate)                                                                monthNo FROM Opportunity WHERE createdDate = THIS_YEAR GROUP BY CALENDAR_MONTH(createdDate)];
        
        for (AggregateResult ag : groupedResults)  
        {
            Integer MonthNo = integer.valueof(ag.get('monthNo'));
            Integer MonthCount = integer.valueOf(ag.get('monthCount'));
            opportunityList.get(MonthNo-1).count = MonthCount;
        }                
    }
    //Wrapper Class
    public class OpportunityInfo
    {
        public String month{get;set;}
        public integer count{get;set;}    
    }
    
    
}


Visualforce Page:


<apex:page standardController="Opportunity" extensions="OpportunitiesByMonthwiseCLS" showheader="false" sidebar="false">
    <apex:pagemessages ></apex:pagemessages>  
    <apex:pageblock >
    <apex:pageblockSection title="Oppotunities by Month wise"> 
        <apex:pageBlocktable value="{!opportunityList}" var="opinfo" >
            <apex:column value="{!opinfo.month}" headervalue="Month"/>
            <apex:column value="{!opinfo.count}" headervalue="No of Opprotunities created"/>
        </apex:pageBlocktable>
        </apex:pageblockSection>
    </apex:pageblock>
</apex:page>


Out put:

save image




0 comments:

Post a Comment

 
| ,