Saturday 27 May 2017

Use Wrapper Class to show data on the Visualforce Page Using Controller

1. I am displaying accounts record which have rating not equals to NULL.
2. To show data on page , I create wrapper class and defines three properties and assign account field values in to these properties.
3. I create a method to get day name from account createddate and assign in to wrapper class property by passing parameter createddate of account.


Visualforce Page:

<apex:page controller="UseWrapperCtr" sidebar="false" showHeader="false">
  <apex:pageBlock>
      <apex:pageBlockTable value="{!lstAccWrapper}" var="acc">
          <apex:column headerValue="Account Name" value="{!acc.accName}"/>
          <apex:column headerValue="Rating" value="{!acc.ratingName}"/>
          <apex:column headerValue="Day Name" value="{!acc.dayName}"/>
      </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

Apex Class:

public class UseWrapperCtr
{
    public List<Wrapper> lstAccWrapper{get;set;}   // Use this wrapper to show data on the page.
    public UseWrapperCtr()
    {
        lstAccWrapper=new List<Wrapper>();
        List<Account> accounts=[Select Id,name,rating,createddate from Account where rating!=null];
        
        //Iterate account and assign value in wrapper.
        for(Account a:accounts)
        {
           Wrapper w=new Wrapper();
           w.accName=a.Name;
           w.ratingName=a.rating;
           w.dayName=returnDayName(a.createddate);
           lstAccWrapper.add(w);
        }
    }
    
    // Create wrapper class
    public class Wrapper
    {
        public String accName{get;set;}
        public String ratingName{get;set;}
        public String dayName{get;set;}
    }
    
    // Find the day name by providing date.
    public String returnDayName(DateTime accCreateDate)
    {
        String dayName=accCreateDate.format('EEEE'); // get the day name by created date of account.
        return dayName;
    }
}



0 comments:

Post a Comment

If you have any doubts, please let me know.