Posted in APEX, Salesforce Interview Questions, Visualforce

Salesforce Wrapper Class

Salesforce Wrapper Class: 

What is Wrapper class?
A Wrapper class is a class whose instances are a collection of other objects. It is used to display different objects on a Visual Force page in the same table. 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 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.

Use of Wrapper Class in Salesforce: Using Wrapper class for displaying records from two or three or more objects in a single table (based on the business logic).

1.Sometimes we need to display data from different objects (not related to each other) in a table.

2.The user wants to display a table of records with a check box and then process only the records that are selected in visual force page. So here am sharing this example:

                                            images (2)

Wrapper class example:

Apex Controller:

public class wrapperClasscontroller {

//Our collection of the class/wrapper objects aAccount
public List<aAccount> AccountList {get; set;}

//This method uses a simple SOQL query to return a List of Accounts
public List<aAccount> getAccounts() {
if(AccountList == null) {
AccountList = new List<aAccount>();
for(Account c: [select Id, Name, Website, Phone from Account where Phone !=null AND Website!=null limit 10]) {
// As each Account is processed we create a new aAccount object and add it to the AccountList
AccountList.add(new aAccount(c));
return AccountList;
public PageReference processSelected() {

//We create a new list of Accounts that we be populated only with Accounts if they are selected
List<Account> selectedAccounts = new List<Account>();

//We will cycle through our list of aAccounts and will check to see if the selected property is set to true, if it is we add the Account to the selectedAccounts list
for(aAccount cacc: getAccounts()) {
if(cacc.selected == true) {

// Now we have our list of selected Accounts and can perform any type of logic we want, sending emails, updating a field on the Account, etc
System.debug(‘These are the selected Accounts…’);
for(Account acc: selectedAccounts) {
AccountList=null; // we need this line if we performed a write operation because getAccounts gets a fresh list now
return null;
// This is our wrapper/acctainer class. A acctainer 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 acctains both the standard salesforce object Account and a Boolean value
public class aAccount {
public Account acc{get; set;}
public Boolean selected {get; set;}

//This is the acctructor method. When we create a new aAccount object we pass a Account that is set to the accproperty. We also set the selected value to false
public aAccount(Account c) {
acc= c;
selected = false;


download (1)

<apex:page controller=”wrapperClassController”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value=”Process Selected” action=”{!processSelected}” rerender=”table”/>
<!– In our table we are displaying the aAccount records –>
<apex:pageBlockTable value=”{!Accounts}” var=”c” id=”table”>
<apex:column >
<!– This is our selected Boolean property in our wrapper class –>
<apex:inputCheckbox value=”{!c.selected}”/>
<!– This is how we access the contact values within our aAccount container/wrapper –>
<apex:column value=”{!c.acc.Name}” />
<apex:column value=”{!c.acc.Website}” />
<apex:column value=”{!c.acc.Phone}” />

And finally output comes here:


You can follow more example for how to create Wrapper class in Apex here



I am 3X Salesforce Certified. Most important thing which I like about Salesforce is giving back. There aren’t enough words in the dictionary to describe the significance of giving back.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s