Creating Wrapper Class in Salesforce Visualforce Pages
I know this Wrapper Class fears so here we are with daily life examples to make it simpler.
Alright, first we have to,
- Take a box-like any carton box. Make sure it's an empty one.
- Prepare the wrapped dough of any dish you want.
- Cut the green leaves, tomatoes, and all that stuff you want to add.
- Fry the veggies and stuff.
- Wrap all of the above in a bubbled hot dough.
- Place the crispy hot wrap in the carton box. (Repeat the above steps for making more wraps)
Don’t forget to check out: How to Share Salesforce Visualforce Pages Between Classic and Lightning Experience?
Now you can understand why we have taken a carton box(OuterClass) and the 5 wraps (InnerClass) present inside the box are secured as shown below.
Now we have seen what Wrapper Class actually is.
Let's see:
1. What is a Salesforce Wrapper class?
A Wrapper class is a container (carton box) class, a data structure, or an abstract data type whose instances are collections of other objects. You can wrap different types of objects in the wrapper class.
2. Why do you need a Wrapper class?
Visualforce page:
<apex:page controller="ContactSelectClassController" sidebar="false" showHeader="false"> <script type="text/javascript"> function selectAllCheckboxes(obj,receivedInputID) { var inputCheckBox = document.getElementsByTagName("input"); for(var i=0; i<inputCheckBox.length; i++) { if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1) { inputCheckBox[i].checked = obj.checked; } } } </script> <apex:form > <apex:pageBlock > <apex:pageBlockButtons > <apex:commandButton value="Show Selected Contacts" action="{!processSelected}" rerender="table2"/> </apex:pageBlockButtons> <apex:pageblockSection title="All Contacts" collapsible="false" columns="2"> <apex:pageBlockTable value="{!wrapContactList}" var="contWrap" id="table" title="All Contacts"> <apex:column > <apex:facet name="header"> <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/> </apex:facet> <apex:inputCheckbox value="{!contWrap.selected}" id="inputId"/> </apex:column>wrapper class <apex:column value="{!contWrap.cont.Name}" /> <apex:column value="{!contWrap.cont.Email}" /> <apex:column value="{!contWrap.cont.Phone}" /> </apex:pageBlockTable> <apex:pageBlockTable value="{!selectedContacts}" var="c" id="table2" title="Selected Contacts"> <apex:column value="{!c.Name}" headerValue="Contact Name"/> <apex:column value="{!c.Email}" headerValue="Email"/> <apex:column value="{!c.Phone}" headerValue="Phone"/> </apex:pageBlockTable> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>
Apex Class:
public class ContactSelectClassController { //Collection of the class/wrapper objects public List<wrapContact> wrapContactList {get; set;} public List<Contact> selectedContacts{get;set;} public ContactSelectClassController(){ if(wrapContactList == null) { wrapContactList = new List<wrapContact>(); for(Contact con: [select Id, Name,Email, Phone from Contact limit 10]) { wrapContactList.add(new wrapContact(con)); } } } public void processSelected() { selectedContacts = new List<Contact>(); for(wrapContact wrapContactObj : wrapContactList) { if(wrapContactObj.selected == true) { selectedContacts.add(wrapContactObj.cont); } } } // wrapper/container class public class wrapContact { public Contact cont {get; set;} public Boolean selected {get; set;} //Constructor method public wrapContact(Contact con) { cont = con; selected = false; } } }
We have collections like "list" which can store only one type of data, suppose we have 'Account' or ‘Contact’. List<Account> will hold only accounts and List<Contact> will hold only contacts in it. But what if you want to show data on the table that should show account records as well as address data? iterating over List<Account> will only allow account records and no contact data.
To solve this problem, we can create a wrapper class having variables account and contact and then create the list of that wrapper class. This may sort out your problem easily might look difficult in starting but as soon as you start using wrapper class you will try to again use it.
Let’s take an example of Wrapper Class in the VisualForce page, in which we are displaying a list of contacts with the checkbox. End-user can select a contact and then click on the Show Selected contacts button. Then the selected contact will be displayed on the table.