Wrapper Class


A Wrapper Class is an extremely useful class that you can use when you need to group a number of objects in one “container”. You will be using this class especially in Visualforce controllers.

A more technical definition will be something like:

“A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects”

It is just a temporary container that can hold objects with their records for further calculations or logics within your code structure.

We are going to build a table in a Visualforce page and display Contact and Account information in that same table using a wrapper class. Here is the initial class, class name WrapperDemoController and like you can see it contains on class wich it is our wrapper class, he named TableRow.


public class WrapperDemoController{
   //Our wrapper class
   public class TableRow{
     public String Name   {get; set;}
     public String Phone  {get; set;}
     public String Company{get; set;}
   }
}

This TableRow class, our wrapper class, also contains three properties of data type String.
If you don’t know what properties are in Apex I suggest that you check this out before continuing.

The names of these properties are an indication of what it is that we want to retrieve and expose in our table, they are, lest say, the column’s names we are going to populated latter on our page.

We are using a list that will hold the TableRow values. Yes, it is a property …

//Public property that will hold all the TableRows.
List<TableRow> row = List {get; set;}

Now we are going to build our constructor that will contain the rest of the components we need to successfully run our wrapper class. Don’t know what a constructor is? here you get a pretty good explanation.

Here is the part with the constructor.

/*
Constructor that runs s SOQL to get all the records and build the list we need.
This gets 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, Accoun.Name FROM Contact]){

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

//Add the TableRow to the list
   rowList.add(tr); 
  }

}

Within our constructor, we utilize our list and our wrapper class by creating a new list of TableRow and a new object from our TableRow as well. Now we can run our SOQL query and retrieve the fields we need for our Visualforce page, Name, Phone, and Account.Name. We want to iterate through contacts, so that is why we need to use a “for loop”. Now we assign the values coming from our SOQL query to our “tr” object. If you remember, our “tr” object has been built from our Wrapper class (TableRow).

As you also remember, our TableRow wrapper class has three properties; Name, Phone, and Company. All we are doing in our for loop is to tell our code to do the following: ‘Eh, for each of the records coming from our query I want to you to give the values of Name (from our query), to tr.Name (from our object). That is all we are doing.
Because it is a for loop, it will do this operation for every simple record coming from that SOQL query.
As you can see, we assigned the values coming from our SOQL query by using the dot notation as follow:
tr.Name = con.Name;

To make it simple, we are asking to pass the value “Name” coming from the SOQL query “con”, to the property Name in our wrapper object “tr”. I hope is ok to call “tr” our wrapper object:), probably is not, but you know what I mean.

All we need to do is to pass everything to our list and we will have our records ready for exposing them in a Visualforce page. RowList.add(tr) is what does this.

Find the full code down bellow:

//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 an 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);
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *