Wednesday 5 September 2012

Wrapper Class in Apex/Visual force page

Wrapper class is a very powerful concept that can be used in multiple scenarios. Putting aside the word "Wrapper" it is nothing but just a class.Just like what we have in a class ,we  have methods,Properties etc in a wrapper class too.Here is an use case for wrapper class

Requirement : A Visual force page has to display all the Contacts available for an Account .Contacts should be listed out in the table row wise(default being column wise) . Only created "date"(Strip the time part) of the contact should be displayed .

Sample page:









Apex Code :

  1. public class displayContacts  
  2.    {    
  3.         
  4.         
  5.       //Get the respective Account id  
  6.       ID accountId;  
  7.        
  8.       //Store the wrapped Contacts  
  9.       public List<contactWrapper> conWrapper{get;set;}  
  10.       
  11.      //COnstructor   
  12.      public displayContacts()  
  13.       {     
  14.          accountId= string.escapeSingleQuotes(ApexPages.currentPage().getParameters().get('id'));    
  15.        
  16.      //Initialize list  
  17.      conWrapper = new List<contactWrapper>();  
  18.      for(Contact tempCon :[select FirstName,LastNAme,Email,phone,createddate from contact where accountid=:accountid])  
  19.      {   
  20.         //create the object for the wrapper class  
  21.         contactWrapper tempWrapper = new contactWrapper(tempCon);  
  22.         conwrapper.add(tempWrapper);  
  23.      }  
  24.        
  25.        
  26.     
  27.       } //End of Constructor  
  28.        
  29.       //Wrapper class for Contact  
  30.       public class contactWrapper  
  31.       {        
  32.          public String contactName{get;set;}  
  33.          public String createdDate{get;set;}  
  34.          public string phoneC{get;set;}  
  35.          public string emailC{get;set;}  
  36.                    
  37.          public contactWrapper(Contact con)  
  38.            {  
  39.              contactName=con.FirstName+' '+con.LastName;  
  40.              createdDate=con.createddate.month() +'/'+ con.createddate.day() +'/'+ con.createddate.year();  
  41.              phoneC=con.phone;  
  42.              emailC=con.email;  
  43.                 
  44.            }//End of Wrapper constructor  
  45.    
  46.      }//End of wrapper class   
  47.            
  48.    } //End of Class  


Visual force page :
  1. <apex:page controller="displayContacts">  
  2.    <apex:pageBlock>  
  3.     <apex:pageBlockSection title="Contacts Available">  
  4.             <apex:dataTable value="{!conWrapper}" var="item1" border="1" columnsWidth="65px,100px" rowClasses="odd,even" styleClass="tableClass">  
  5.             <apex:column width="200px">  
  6.                     <table width="250" border="0">                       <tr><td><b>Name :</b></td><td>{!item1.contactName }</td></tr>                        <tr><td><b>Phone :</b></td><td>{!item1.phonec}</td></tr>                         <tr><td><b>Email :</b></td><td>{!item1.emailc} </td></tr>                        <tr><td><b>Created Date :</b></td><td>{!item1.createdDate} </td></tr>                     </table>               </apex:column>   
  7.             </apex:dataTable>  
  8.     </apex:pageBlockSection>  
  9.     </apex:pageBlock>  
  10. </apex:page>  


Result :












Huh!! now please don't catch a bug here that no values are displayed for phone. I admit i missed them..Pretty tired ...accept my apology and take it for granted :) :)

Power of wrapper is seamless .It can be used to process only the records that are selected in vf page.Click on the link for example.

http://wiki.developerforce.com/index.php/Wrapper_Class

You Want More Information about Salesforce.com Please Click Here

1 comment: