How to use wrapper class in the apex

Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9 ** Article 10

Before we see an example of how wrapper class is used we should know what is wrapper class,why wrapper class is used and what makes it required.

What is a wrapper class?

A wrapper class i would say is a custom object defined by programmer wherein he defines the wrapper class properties. Consider a custom object in salesforce, what do you have in it? fields right? different fields of different data types. On similar grounds i can say wrapper class has different data types/properties as desired by the programmer. You can wrap different objects types or any other types in a wrapper class.

Why 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 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 list of that wrapper class. This may look a bit complicated but as you go through an example it will sort out easily.

An example i can think of is.. You want to display a account data in table along with a check box on the right side of the table(checkbox for every row) Well you can very well bind a account checkbox field to this right column but below example is for demonstrating wrapper with a custom checkbox column.

Your table value will iterate through list of wrapper class.For every account and checkbox value(false) You will be storing a new wrapper class records in the wrapper class list. This wrapper calss has properties (Account and Boolean).

-- Visualforce page --

<apex:page standardController="Account" extensions="WrapperDemoClass">
  <apex:form >
    <apex:pageblock >
      <apex:pageblockTable value="{!wrapperObj}" var="Rec">
         <apex:column value="{!}"/>
         <apex:column >
            <apex:inputcheckbox value="{!Rec.checkBox}"/>

-- Controller used --

public with sharing class WrapperDemoClass {
 Public List<WrapperClassEx> WrapperList{get;set;}
    public WrapperDemoClass(ApexPages.StandardController controller) {
   Public List<WrapperClassEx> getwrapperObj(){
      List<Account> accList = [Select id,name from account limit 5];
      WrapperList = New List<WrapperClassEx>();
      for(Account acc: accList){
        WrapperList.add(New WrapperClassEx(acc,false)); 
      return WrapperList;
   Public Class WrapperClassEx{
     Public Account accObj{get;set;}
     Public Boolean checkBox{get;set;}
     Public WrapperClassEx(Account accRec, boolean SelectBox){
        accObj = accRec;
        checkBox = SelectBox;
Article 1** Article 2** Article 3** Article 4** Article 5** Article 6** Article 7** Article 8** Article 9 ** Article 10

Click For : Salesforce Interview Question And Answers


  1. Very nice! can think of many uses for this pattern.

  2. Superb Explanation, Great work yar...

  3. Really nice and easily understandable explanation
    Thank you so much for posting this.. :-)

  4. Well explained!!!!! Hats off !!!!! You have a great future in salesforce!!!!!

  5. Really impressed Yoganand!!!! Yes, you have a bright future!!!!! keep it up!!!!

  6. Hello Sir, the output of the above code is just displaying the records.. can we use some custom button to save them in a separate list or something??

  7. Nice explanation of wrapper clas, with just few lines of code.

  8. Really Nice Post, wonderful explanation of wrapper class with few lines of code

  9. super way of teaching.but in this if we want that on clicking the checkbox the detail of that contact is display then what will be the code plzzz send this

  10. thanks sir but if we want that on clicking the checkbox the detail of that particular accont isdisplay then what will be the code

  11. well explained

  12. This is best explanation for wrapper class so far I have seen, thanks man....

  13. Wrapper class output is not to expected as per interviewers in 2017.
    Any chances of updating ??