Ask Search:
RBS SFRBS SF 

What is wrapper class?? How to use wrapper class in the apex??

Best Answer chosen by RBS SF
Deepak AnandDeepak Anand
Let me put ma 2 cents.....Say you have to create a Table in your Visualforce Page in other words a PageBlockTable(apex:pageBlockTable). Each row of the table is suppose to show information from a Contact record also some details from the related Account record too. So, it would be nice, it we had a Class(An Abstract Data Type or a wrapper or a kind of a temporary holder for information) where it could hold the details for each row from different tables:

public class TableRow{
        public String Name      {get;set;}
        public String Phone     {get;set;}
        public String Company   {get;set;}
}

Now, your Apex Controller class will have this:

public class WrapperDemoController{
    /*Our Wrapper Class*/
    public class TableRow{
        public String Name      {get;set;}
        public String Phone     {get;set;}
        public String Company   {get;set;}
    }
   
    /*Public Property that will hold all the TableRows and our PageBlockTable can use this Variable   to get the whole List of rows*/
    public List<TableRow> RowList {get; set;}

    /*Constructor that runs a SOQL to get all the Records and build the List. This get executed automatically on the Page Load.*/
    public WrapperDemoController(){

        RowList = new List<TableRow>();
        TableRow tr;

        /*Building the List of TableRows*/
        /*Fetch all the Contacts and then build the List*/
        for(Contact con : [SELECT Name, Phone, Account.Name FROM Contact]){

            tr = new TableRow();
            tr.Name = con.Name;
            tr.Phone = con.Phone;
            tr.Company = con.Account.Name;

            /*Add the TableRow to the List then and there*/
            RowList.add(tr);
        }
    }
}

Now the Visualforce Markup will be:

<apex:page controller="WrapperDemoController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!RowList}" var="row">
            <apex:column value="{!row.Name}"/>
            <apex:column value="{!row.Phone}"/>
            <apex:column value="{!row.Company}"/>
        </apex:pageBlockTable>   
    </apex:pageBlock>
</apex:page>

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

All Answers

Shivanath DevinarayananShivanath Devinarayanan
A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In Apex and Visualforce this type of class can be extremely useful but to a new developer the talk of wrapper classes may fly right over your head, it definitely did for me. Here I will supply a simple demo of how wrapper classes can be used.

Read more : http://wiki.developerforce.com/page/Wrapper_Class

 
 if our suggestion(s) worked,  let us know by marking the answer as "Best Answer" right under the comment.This will help the rest of the community should they have a similar issue in the future.  Thank you!

Shivanath
RBS SFRBS SF
Yes i already study this notes but i am confusing with this one..is thier any other source..
Shivanath DevinarayananShivanath Devinarayanan
Have you tried this ? http://theblogreaders.com/what-is-wrapper-class-and-use-of-wrapper-class-in-salesforce/#.UrAZd2QW1vk (http://theblogreaders.com/what-is-wrapper-class-and-use-of-wrapper-class-in-salesforce/#.UrAZd2QW1vk)
RBS SFRBS SF
Thank you..
Shivanath DevinarayananShivanath Devinarayanan
Glad to be of help !
Shivanath DevinarayananShivanath Devinarayanan
if our suggestion(s) worked,  let us know by marking the answer as "Best Answer" right under the comment.This will help the rest of the community should they have a similar issue in the future.  Thank you!

Shivanath
Deepak AnandDeepak Anand
Let me put ma 2 cents.....Say you have to create a Table in your Visualforce Page in other words a PageBlockTable(apex:pageBlockTable). Each row of the table is suppose to show information from a Contact record also some details from the related Account record too. So, it would be nice, it we had a Class(An Abstract Data Type or a wrapper or a kind of a temporary holder for information) where it could hold the details for each row from different tables:

public class TableRow{
        public String Name      {get;set;}
        public String Phone     {get;set;}
        public String Company   {get;set;}
}

Now, your Apex Controller class will have this:

public class WrapperDemoController{
    /*Our Wrapper Class*/
    public class TableRow{
        public String Name      {get;set;}
        public String Phone     {get;set;}
        public String Company   {get;set;}
    }
   
    /*Public Property that will hold all the TableRows and our PageBlockTable can use this Variable   to get the whole List of rows*/
    public List<TableRow> RowList {get; set;}

    /*Constructor that runs a SOQL to get all the Records and build the List. This get executed automatically on the Page Load.*/
    public WrapperDemoController(){

        RowList = new List<TableRow>();
        TableRow tr;

        /*Building the List of TableRows*/
        /*Fetch all the Contacts and then build the List*/
        for(Contact con : [SELECT Name, Phone, Account.Name FROM Contact]){

            tr = new TableRow();
            tr.Name = con.Name;
            tr.Phone = con.Phone;
            tr.Company = con.Account.Name;

            /*Add the TableRow to the List then and there*/
            RowList.add(tr);
        }
    }
}

Now the Visualforce Markup will be:

<apex:page controller="WrapperDemoController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!RowList}" var="row">
            <apex:column value="{!row.Name}"/>
            <apex:column value="{!row.Phone}"/>
            <apex:column value="{!row.Company}"/>
        </apex:pageBlockTable>   
    </apex:pageBlock>
</apex:page>

So, in simpler words wrapper could be thought of as temporary holder/buffer for information lying across different tables queried via a SOQL.
This was selected as the best answer
RBS SFRBS SF
Deepak you really awesome explanation..Now i am clear what is wrapper..and at now my requirement is i want to bind the each row associated with one check box when i enable that then selected checkbox records will be display at bottom of the page.. how can i achieve this one..Thank you..
Ankush DurejaAnkush Dureja
A wrapper or container class is a class, a data structure, or an abstract data type which contains different objects or collection of objects as its members.
A wrapper class is a custom object defined by programmer wherein he defines the wrapper class properties.

See below link for more details:

http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/
Devendra KeshariDevendra Keshari
i am new in salesforce i am unable to unstand wrapper classes
Neha RahangdaleNeha Rahangdale
Wrapper class is a collection of different data types which can be use for different purpose.
I want to store account record corresponding primary contact.If I take list<Account> then I can add only accounts.
In this case I have to create wrapper class with account and  contact as a variable.
 ex:If I want to display Account record with checkbox.