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.
In the Visualforce community boards one of the most commonly asked questions is, "How can I display a table of records with a check box and then process only the records that are selected?", like this:
This is a perfect scenario where a wrapper class can be used. Most of my instructions and comments are in the code but anyone should feel free to modify this entry to make it easier to understand.
First the Controller:
01 | public class wrapperClassController { |
03 | //Our collection of the class/wrapper objects cContact |
04 | public List <cContact > contactList { get ; set;} |
06 | //This method uses a simple SOQL query to return a List of Contacts |
07 | public List <cContact > getContacts() { |
08 | if (contactList = = null ) { |
09 | contactList = new List <cContact > (); |
10 | for (Contact c: [ select Id, Name, Email, Phone from Contact limit 10 ]) { |
11 | // As each contact is processed we create a new cContact object and add it to the contactList |
12 | contactList.add( new cContact(c)); |
19 | public PageReference processSelected() { |
21 | //We create a new list of Contacts that we be populated only with Contacts if they are selected |
22 | List <Contact > selectedContacts = new List <Contact > (); |
24 | //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list |
25 | for (cContact cCon: getContacts()) { |
26 | if (cCon.selected = = true ) { |
27 | selectedContacts.add(cCon.con); |
31 | // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc |
32 | System .debug( 'These are the selected Contacts...' ); |
33 | for (Contact con: selectedContacts) { |
36 | contactList= null ; // we need this line if we performed a write operation because getContacts gets a fresh list now |
41 | // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value |
42 | public class cContact { |
43 | public Contact con { get ; set;} |
44 | public Boolean selected { get ; set;} |
46 | //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false |
47 | public cContact(Contact c) { |
And then the Page:
01 | <apex:page controller= "wrapperClassController" > |
04 | <apex:pageBlockButtons > |
05 | <apex:commandButton value= "Process Selected" action= "{!processSelected}" rerender= "table" / > |
06 | </apex:pageBlockButtons > |
07 | <!-- In our table we are displaying the cContact records -- > |
08 | <apex:pageBlockTable value= "{!contacts}" var= "c" id= "table" > |
10 | <!-- This is our selected Boolean property in our wrapper class -- > |
11 | <apex:inputCheckbox value= "{!c.selected}" / > |
13 | <!-- This is how we access the contact values within our cContact container/wrapper -- > |
15 | <apex:column value= "{!c.con.Email}" / > |
16 | <apex:column value= "{!c.con.Phone}" / > |
17 | </apex:pageBlockTable > |
Additional Examples Using Wrapper Classes:
Using a wrapper class to control styles