+ Start a Discussion
AjjuAjju 

what is a Wrapper class ? plz explain with example?

thisisnotaprilthisisnotapril
Hi Ajju, you need to spend some time looking over the training materials we have on this site. https://developer.salesforce.com/page/Wiki
Ashish_SFDCAshish_SFDC
Hi Ajay, 


A wrapper or container class is a class, data structure, or an abstract data type whose instances are a collections of other objects.It is a custom object defined by Salesforce developer where he defines the properties of the wrapper class. Within Apex & Visualforce this can be extremely helpful to achieve many business scenarios within the Salesforce CRM software.

Using Wrapper classes we can have the ability to check few records from the displayed list and process them for some action. 

See the example below, 

http://2.bp.blogspot.com/-p8zeAeNXvNg/UgtB0_IbsNI/AAAAAAAAAqM/XVaKMjBu2s4/s1600/visualforce_wrapper_class.JPG

Also see the blogs for the wrapper class code below and further information, 

http://sfdcgurukul.blogspot.sg/2013/08/what-is-wrapper-classhow-to-use-it-in.html

http://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/


Regards,
Ashish


nitesh gadkarinitesh gadkari
-You can understand Wrapper like this.If you want to make a object which includes both( account and contact) or( account and its corresponding selection check box) you use wrapper class

-i am giving code . only  selected accounts are showed in the right side.
<apex:page sidebar="false" controller="WrapNitesh">
   
 <!--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>
public class WrapNitesh {

//CONTROLLER CLASS

    public list<wrapaccount> wrapaccountList { get; set; }
    public list<account> selectedAccounts{get;set;}    

      
      public WrapNitesh (){
      
     //if(wrapaccountList ==null){
          wrapaccountList =new list<wrapaccount>();
          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;}
     
       public wrapaccount(account a){
     
         accn=a;
         isselected=false;
       }
  }
}


 
Cloud_forceCloud_force
Wrapper class in simple words is a custom defined type by programmer, whose structure will be defined as required by programmer. For example you may want to define a type that will conatin some account data+some images+some other custom object data. In order to accomodate this in a signle type you cannot use any standard salesforce type [list or set or any object] you will have to define your own type and that will be wrapper class.


thanks,
http://www.forcexplore.com/2014/01/salesforce-interview-questions-2.html
Karamala DamodarKaramala Damodar
wrapper is class or container  whos instance is collection of other objects
the best example is u applied any jobs in websites the page look like checkbox job description aplly button three are different objects but all of then displayed in single page that is wrapper class
Maf_007Maf_007
Hi Ajju,

After all above discussion only thing I can add is, Wrapper class is construction of an object in Apex code which can combine fields from different objects or a bunch of fields which you need only in Run time to achieve your goal. 
Sudeep DubeSudeep Dube
Hi,
      Wrapper Class is collection of two or more diffrent objects. 
      So these are the abstract data structure by which a developer create its own data structure by groupuing another available data structure.
      It expands user capablity and give more power to developer a customized application. it can be user for creating Visualforce page where page contains Boolean as check box and list item as value..
                     
    
Shephali SwarnkarShephali Swarnkar
Hi nitesh gadkari,
     Thanks so much for this code.it works
Pavan kumar 546Pavan kumar 546
this was a fantastic explanation
 to learn concept easily
Amit SAmit S
Need help ppl!

I have a requirement to configure a Custom "View All" Button in Accounts View Related list for the Activities History.

On Click of the Button, The View should contain a List of Activities(Tasks) and a Long Text Area field from a Custom Object for each activity. 

Acvitity Record Id is stored in the Custom Object Field.

Can I use a Wrapper Class and add a list/field from the Custom Object instead of the Flag? Has anyone worked on a similar requirement. 
Arpita NayakArpita Nayak
Hi Nitesh,
Your example is very helpful.I tried this but i am unable to understand the flow.Can you please briefly explain the wrapper class code.
Adil_SFDCAdil_SFDC
This gave me a good idea on the wrapper classes. Thanks All 
INDERJEET S SINGERKHANIINDERJEET S SINGERKHANI
Thanks Everyone....!
Carl NielsenCarl Nielsen
So an apex class is similar to creating a ViewModel in an MVC app? If the page to be rendered is different to the domain model, controller must put all the relevant data into/outof a ViewModel class before rendering view / persisting to data store.
Arun Garg 9Arun Garg 9
Wrapper class Best Example with Code.
Use Wrapper Class to show data on the Visualforce Page Using Controller
https://sfdcadda.blogspot.in/2017/05/use-wrapper-class-to-show-data-on.html
Sekar Raj 8Sekar Raj 8
Wrapper class with checkbox(select option) example.
https://cloudtechnzly.blogspot.in/2017/08/wrapper-class-examples-in-salesforce.html
farukh sk hdfarukh sk hd
wrapper class is class whose instances are collection of objects.

Please visit for more detail,

https://www.sfdc-lightning.com/2018/10/wrapper-class-in-salesforce.html