Saturday, August 4, 2018

How to create Wrapper class in salesforce

public with sharing class testwrappercontroller {

    //createing a list for wrapper class object
    // always try to create list seperately outside
//    constructor and return it rseperately
//the key concept here is to create list and other variablesotuside
//the constructor.remeber the constructor does not always create all list variable
//variable get methods fire independently of constrcutor,where everthe the get set is defined for
//the variable,in the below case the wpl variable fires seperately
//when it sinvoed as {!wpl}
   public  List<wrapperclass> wpl {get
 
   {
 
    Account ac =[Select Id,name from Account limit 1];
    Contact co =[Select Id,name from Contact limit 1];
    wpl = new List<wrapperclass>();
    wpl.add(new wrapperclass(ac,co));
     return wpl;}
   
      set;}
 
    public void testwrappercontroller(){
    system.debug('hi');  
   
 

    }
   
 
   
    public class wrapperclass{
    public Account acc {get; set;}
    public Contact coo {get; set;}
    public  wrapperclass(Account a,Contact c){  
    acc =a;
    coo =c;  
   
     }
    }
   
   
}


<apex:page controller="testwrappercontroller" tabStyle="account">
  <apex:pageBlock >
  <apex:pageBlockSection title="Section" columns="2"/>
      <apex:pageBlockTable value="{!wpl}" var="cw"  >    
        <apex:column value="{!cw.acc.name}" rendered="true"/>
        <apex:column value="{!cw.coo.name}" rendered="true"/>
 
     
      </apex:pageBlockTable>
   
    </apex:pageBlock>
   
   
 
</apex:page>