Wednesday 23 August 2017

Wrapper Class

                                   What is the wrapper class

1. What is the Wrapper class:  A Wrapper class is a container class, a data structure, or an abstract data type whose instances are collections of other objects. You can wrap different objects types or any other types in a wrapper class.

  in simpler words, wrapper could be thought of as temporary holder/buffer for information lying across different tables queried via a SOQL.


1. Why we used wrapper class: Generally collections like "list" can store only one type of data for example "string' or "Account". List<Account> will hold only accounts and List<string> will hold only strings in it. But what if you want to show data on the table that should show account record as well as it primary contact data  ? iterating over List<Account> will only allow account records and no contact data. 


In this case, you can create a wrapper class having variables account and contact and then create the list of that wrapper class. This may look a bit complicated but as you go through an example it will sort out easily.


   Example of wrapper class:

Apex class:


public class Wrapharsh {

//CONTROLLER CLASS

   //Our collection of the class/wrapper objects wrapaccount
    public list<wrapaccount> wrapaccountList { get; set; }

    //We create a new list of Accounts that we be populated only with Accounts if they are selected
    public list<account> selectedAccounts{get;set;}    

      
      public Wrapharsh ()
    {
          wrapaccountList =new list<wrapaccount>();

         // As each account is processed we create a new wrapaccount object and add it to the accountlist
          for(account a:[select id,name,billingcity,phone from account limit 10]){
           wrapaccountlist.add(new wrapaccount(a));
        
           }
      }

    //### SELECTED ACCOUNT SHOWN BY THIS METHOD
      public void ProcessSelected(){
     selectedAccounts=new list<account>();
     
      for(wrapaccount wrapobj:wrapaccountlist){
           if(wrapobj.isSelected==true){
           selectedAccounts.add(wrapobj.accn);
           }
            
         }
      }
      
  //##THIS IS WRAPPER CLASS
   // account and checkbox taken in wrapper class
   
   public class wrapaccount{
    
    public account accn{get;set;}
    public boolean isSelected{get;set;}
   
     //This is the contructor method. When we create a new wrapaccount object we pass a Account that is set to the accn property. We also set the selected value to false
       public wrapaccount(account a){
     
         accn=a;
         isselected=false;
       }
  }
}

Visual force Page:


<apex:page controller="Wrapharsh" sidebar="false" >
   
 <!--VF PAGE BLOCK-->

 <apex:form >
     <apex:pageBlock >
        <apex:pageBlockButtons >
          <apex:commandButton action="{!ProcessSelected}" value="Show Selected accounts" reRender="block2"/>
        </apex:pageBlockButtons>
       <apex:pageBlockSection columns="2">
         <apex:pageBlockTable value="{!wrapaccountList}" var="waccl">
            
           <apex:column >
             <apex:facet name="header">
               <apex:inputCheckbox />
             </apex:facet>
            <apex:inputCheckbox value="{!waccl.isSelected}" id="InputId"/>
           </apex:column>
            
            <apex:column value="{!waccl.accn.name}"/>
            <apex:column value="{!waccl.accn.phone}"/>
            <apex:column value="{!waccl.accn.billingcity}"/>
         </apex:pageBlockTable>
         
          <apex:pageBlockTable value="{!selectedAccounts}" var="sa" id="block2">
            <apex:column value="{!sa.name}"/>
            <apex:column value="{!sa.phone}"/>
            <apex:column value="{!sa.billingcity}"/>
           </apex:pageBlockTable>
       
       </apex:pageBlockSection>
     </apex:pageBlock>
   </apex:form>
</apex:page>

1 comment:

One particular lead takes how many days to change it's stage from one stage value to another

/*************** Ceated By    : Mohit Dwivedi( KVP Business Solution Pvt Ltd) . Created Date :  Purpose      : This  controller is for Lead ...