wrapper class in apex

Wrapper Class in Apex Salesforce | The Developer Guide

Wrapper Class in Apex Salesforce

Wrapper Class in Apex Salesforce: A wrapper or compartment radiance is a class, a realities shape, or a unique data kind that fuses particular articles or assortments of contraptions as its people.

A wrapper style is a custom article depicted by utilizing a software engineer in which he characterizes the wrapper tastefulness houses. Consider a custom thing in salesforce, what do you have in it?

Fields right? One of a kind fields of different statistics sorts. Similarly, wrapper elegance is a custom elegance which has one of a kind statistics types or houses as in keeping with the requirement. We can wrap unique gadgets sorts or every other sort in a wrapper elegance.

dont miss out iconDon't forget to check out: Upload Files as Attachment using Apex Salesforce

In the Visualforce most extreme basic use case is to show a work area of records with a checkbox and afterward framework best the measurements which can be chosen.

In the example beneath, we are showing a listing of debts with a checkbox. End consumers can select an account after which click on the Show Selected debts button. Then the selected account could be displayed in the table.

Visualforce Code

<apex:page controller="AccountSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");            
            for(var i=0; i<inputCheckBox.length; i++){     
                    inputCheckBox[i].checked = obj.checked;
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="table2"/>
            <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">              
                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/> 
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">\
                    <apex:column value="{!c.Name}" headerValue="Account Name"/>
                    <apex:column value="{!c.BillingState}" headerValue="Billing State"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>

Apex Class Controller

public class AccountSelectClassController{
    //Our collection of the class/wrapper objects wrapAccount 
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}     
    public AccountSelectClassController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
    public void processSelected() {
        selectedAccounts = new List<Account>();
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
    // 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 Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;} 
        //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false
        public wrapAccount(Account a) {
            acc = a;
            selected = false;

Benefits of Wrapper Class in Salesforce

A Wrapper Class is frequently used by Salesforce Developers for creating new gadgets inside the purview of Apex code. It allows the customers to consolidate a set of different fields (whether or not they belong to extraordinary items or no longer) which are required the maximum at some stage in runtime.

Here are a portion of the essential gifts of productively utilizing Wrapper Class in Salesforce by developers.

The structure of Wrapper Class is as proficient as that of an incredible insights perception procedure for a site page, for the most part if the engineers are dealing with the JSON shape.

dont miss out iconCheck out another amazing article by Sumit here: Salesforce Workflow Vs Process Builder in Salesforce

The developers aren't required to attempt the death of any guide structure which will peruse important components.

Also, Wrapper Class makes the process of dealing with courting among extraordinary gadgets less difficult for the builders.

The use of Wrapper Class prevents any penalty confronted by way of the users for passing a Salesforce object. Moreover, it makes the worried item extendable to class constructors.

The utilization of Wrapper Class encourages the engineers in sorting out the included measurements effectively, outfitting the insights is pleasantly settled.

These segments suggest that a Wrapper Class can be found together with a Container Class. Be that as it may, it isn't in every case extremely valuable to achieve that on the grounds that keeping up them independently could make it simpler for the engineers to keep them and reuse them as indicated by their prerequisites. It additionally forestalls any duplication of codes that would in some other case make matters confused for the developers.

Popular Salesforce Blogs