Wrapper Class

A wrapper class in Salesforce is a Standard Class Which Contains the Different Data Types That We Used During Development Time. It acts as Container for Multiple Elements: Provides a container to multiple items often coming from different sources or is helpful in giving context and flexibility with the operations applied on complex data streams. Wrapper classes are common in Apex, because they let you define fields from different objects or include extra variables (like boolean for checkbox) along with Salesforce records.

Key features of Wrapper Class?

  1. Combines Multiple Data Types: A wrapper class can hold a variety of data types (strings, integers, boolean, objects) in one class instance.
  2. Custom Objects: Developers can define their custom properties and methods within the wrapper, making it versatile for specific needs.
  3. Interaction with Visualforce Pages: Wrapper classes are often used to pass data between Apex classes and Visualforce pages, making it easier to display complex sets of data in tables or other formats.

Wrapper Class Example 

Wrapper class for displaying Checkbox and String Data types in a single table.

Create a Class with the name “WrapperIntStringDisplayClass” :

public with sharing class WrapperIntStringDisplayClass {

// Creating lists for the object Testing__c and DataLoadTest__c.
List<Testing__c> lsttest = new List<Testing__c>();
List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();

// Creating List for Wrapper class
public List<wrapper> lstw = new List<wrapper>();

// Get method calling from PageBlockTable and return the list of wrapper to Table
public List<wrapper> getLstwrapperIntString() {

lsttest = [select name,city__c from Testing__c];
lstdlt = [select country__c,phone__c from DataLoadTest__c];

for(Integer i=0;i<lstdlt.size();i++){
lstw.add(new wrapper(lsttest[i].name,lsttest[i].city__c,lstdlt[i].country__c,lstdlt[i].phone__c));
}

return lstw;

}

// Wrapper Class Construction

public class wrapper{

public String Tname{get;set;}
public String Tcity{get;set;}
public String Dcountry{get;set;}
public String Dphone{get;set;}

// Wrapper class constructor

public wrapper(String Tname,String Tcity,String Dcountry,String Dphone){
this.Tname=Tname;
this.Tcity=Tcity;
this.Dcountry=Dcountry;
this.Dphone=Dphone;
}

}

}

Step 1: Create Apex Class WrapperIntStringDisplayClass

This Apex class is responsible for retrieving data from two objects (Testing__c and DataLoadTest__c), creating a wrapper class to combine data from both, and passing it to the Visualforce page.

  1. Define List for Objects:
    • Create lists to store data from two custom objects, Testing__c and DataLoadTest__c.
List<Testing__c> lsttest = new List<Testing__c>();
List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();

Define a Wrapper Class:

  • The wrapper class is created to hold both Testing__c and DataLoadTest__c data for display.
public class wrapper{
    public String Tname{get;set;}
    public String Tcity{get;set;}
    public String Dcountry{get;set;}
    public String Dphone{get;set;}

    // Constructor to initialize variables
    public wrapper(String Tname,String Tcity,String Dcountry,String Dphone){
        this.Tname=Tname;
        this.Tcity=Tcity;
        this.Dcountry=Dcountry;
        this.Dphone=Dphone;
    }
}

Create a Method to Retrieve and Populate Data:

  • This method fetches records from both objects and populates the wrapper class by iterating through the records.
public List<wrapper> getLstwrapperIntString() {
    lsttest = [SELECT Name, City__c FROM Testing__c];
    lstdlt = [SELECT Country__c, Phone__c FROM DataLoadTest__c];
    
    for(Integer i = 0; i < lstdlt.size(); i++){
        lstw.add(new wrapper(lsttest[i].Name, lsttest[i].City__c, lstdlt[i].Country__c, lstdlt[i].Phone__c));
    }
    return lstw;
}

Return the Wrapper List to Visualforce Page:

  • The method getLstwrapperIntString is called in the Visualforce page to display the data in a table format.

Create a VF Page with the controller

<!-- Creating this page for dispaly Testing__c and DataLoadTesting__c object in single table with check box -->
<apex:page sidebar="false" controller="WrapperIntStringDisplayClass">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!lstwrapperIntString}"             var="w">
<apex:column headervalue="Action">
<apex:inputcheckbox />
</apex:column>
<apex:column headervalue="TestingName">
{!w.Tname}
</apex:column>
<apex:column headerValue="TestingCity">
{!w.Tcity}
</apex:column>
<apex:column headervalue="DataLoadCountry">
{!w.Dcountry}
</apex:column>
<apex:column headerValue="DataLoadPhone">
{!w.Dphone}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 Explaination

Define Page and Controller:

  • Set the controller as WrapperIntStringDisplayClass and disable the sidebar for simplicity.

Create Form and Table Layout:

  • Use <apex:form> and <apex:pageBlockTable> to display data from the wrapper.

Run and Test

Once the Apex class and Visualforce page are in place, navigate to the page in Salesforce. It will display:

  • table with:
    • A checkbox in the first column.
    • Data from the Testing__c object (TnameTcity) and the DataLoadTest__c object (DcountryDphone) in the remaining columns.

Explanation of Key Components:

  • Wrapper Class: Combines fields from two different Salesforce objects (Testing__c and DataLoadTest__c) into a single data structure for easy display.
  • Visualforce Page: Displays data from the wrapper class in a table format, with each row representing data from both objects and a checkbox for selection.
  • Checkboxes: Allow interaction (selection) on the records displayed.