+ Start a Discussion
Hieu VuongHieu Vuong 

My Wrapper List not show up

I'm trying to create a table to provide metrics on the number of Account and Contact records grouped by state. So each state will have two number of count id.

Below is a code for my class and visual force page

Apex Class:
public with sharing class SampleController {
    public list<stateWrapper> recordlist { get; set;}
   public class stateWrapper {
		public String stateName;
		public Integer contactCount;
		public Integer accountCount;
		
		//Default constructor for innerClass
		public stateWrapper(
			String stateName, 
			Integer contactCount, 
			Integer accountCount
		){
			this.stateName = stateName;
			this.contactCount = contactCount;
			if ( contactCount == null ){
				contactcount = 0;
			}
			this.accountCount = accountCount;
			if ( accountCount == null ){
				accountCount = 0;
			}
			//the above this keyword denotes that we are talking about
			//the variables outside the constructor and not
			//the input parameters by the same name.
		}
	}

	public static Map<String,stateWrapper> calculateMetrics(){
		Map<String,stateWrapper> stateMap = 
			new Map<String,stateWrapper>();
		for ( AggregateResult ar : [	
			Select Count(id) cnt, BillingState state
			from Account 
			where BillingState != null 
			group by BillingState
		]){
			stateWrapper sw = new stateWrapper(
				(string)ar.get('state'),
				(integer)ar.get('cnt'),
				null
			);
			stateMap.put(sw.stateName,sw);
		}
		for ( AggregateResult ar : [	
			Select Count(id) cnt, MailingState state
			from Contact 
			where MailingState != null 
			group by MailingState
		]){
			stateWrapper sw = new stateWrapper(
				(string)ar.get('state'),
				null,
				null
			);
			if ( stateMap.containsKey(sw.stateName) ){
				sw = stateMap.get(sw.stateName);
			}
			sw.contactCount = (integer)ar.get('cnt');
			stateMap.put(sw.stateName,sw);
		}
		return stateMap;
	}
}

This is my visualforce page:
<apex:page controller="SampleController" >
      <apex:pageBlock >
        <apex:pageBlockTable value="{!recordlist}" var="a" title="My Badges" columns="4" align="center"  styleClass="table table-striped">
            <apex:facet name="header">Account and Contact State DashBoard </apex:facet> 
             <apex:column > <apex:facet name="header">State</apex:facet>
            {!a['stateName']}</apex:column>
             <apex:column > <apex:facet name="header">Number of Contacts</apex:facet>
            {!a['contactCount']}</apex:column>
             <apex:column > <apex:facet name="header">Number of Accounts</apex:facet>
            {!a['accountCount']}</apex:column>
        </apex:pageBlockTable>
      </apex:pageBlock> 
  </apex:page>

Screenshot of my visualforce page showing nothing:

User-added image
 
Sunil RathoreSunil Rathore
Hi Hieu,

You have to use the wrapper variable like this. Please refer the below visualforce code:
<apex:page controller="SampleController" >
      <apex:pageBlock >
        <apex:pageBlockTable value="{!recordlist}" var="a" title="My Badges" columns="4" align="center"  styleClass="table table-striped">
            <apex:facet name="header">Account and Contact State DashBoard </apex:facet> 
             <apex:column > <apex:facet name="header">State</apex:facet>
            {!a.stateName}</apex:column>
             <apex:column > <apex:facet name="header">Number of Contacts</apex:facet>
            {!a.contactCount}</apex:column>
             <apex:column > <apex:facet name="header">Number of Accounts</apex:facet>
            {!a.accountCount}</apex:column>
        </apex:pageBlockTable>
      </apex:pageBlock> 
  </apex:page>

Hope this will help you. If does then mark it as the best answer so it can help others in the future.

Many Thanks,
Sunil Rathore