wrapper class

What Is Wrapper Class & How To Use It In Salesforce | The Complete Guide

Well, everyone knows that there a lot of thing to learn in Salesforce development, Here is one basic topic I am going to explain with proper working code, most of the time people get requirements like  same and you can achieve this easily by using Wrapper class in Salesforce:

Definition: Wrapper class is a Container class used to wrap or bind multiple objects in a single class and use all class functionality to show data on a single visual force page in the same table.

dont miss out iconDon't forget to check out: Salesforce Ant Migration Tool - The Complete Guide

Scenario 1: Fetch Account records when the checkbox is selected true

Controller Class:

public class AccountSelectController {
    public List<WrapAccount> wrapAccList{get;set;}
    public List<Account> selectedAccounts{get;set;}
    public AccountSelectController(){
        if(wrapAccList== null){
            wrapAccList= new List<WrapAccount>();
            List<Account> acc=[select id,name,phone from Account];  
            for(Account a: acc){
                wrapAccList.add(new WrapAccount(a));
            }
        }
        system.debug('wrapAccList++'+wrapAccList);
    }
    public void processSelected(){
        selectedAccounts= new List<Account>();
        for(WrapAccount w:wrapAccList){
            if (w.selected==true){
                selectedAccounts.add(w.acc); 
            }
        }
    }
    public class WrapAccount{
        public Account acc{get;set;}
        public Boolean selected{get;set;}
        public WrapAccount(Account a){
            this.acc=a;
            this.selected=false;
        }
    }
}

Visualforce Page for controller:

<apex:page controller="AccountSelectController">
    <apex:form >
    <apex:pageBlock >        
        <apex:pageBlockButtons >
        <apex:commandButton action="{!processSelected}" value="Open Selected Accounts" rerender="t2"/>
        </apex:pageBlockButtons>        
        <apex:pageBlockSection title="CheckBox Selected Accounts">
            <apex:pageBlockTable value="{!wrapAccList}" var="ac">
                <apex:column >
                    <apex:inputCheckbox value="{!ac.selected}"/>
                </apex:column>
                <apex:column value="{!ac.acc.name}"/>
                    <apex:column value="{!ac.acc.Phone}"/>
                        <apex:column value="{!ac.acc.id}"/>
            </apex:pageBlockTable>            
            <apex:pageBlockTable value="{!selectedAccounts}" var="sl" id="t2">
                <apex:column value="{!sl.name}"/>
                <apex:column value="{!sl.phone}"/>
            </apex:pageBlockTable>            
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Scenario 2: Display the all contacts records related with Account when checkbox of Account data is checked true

Here I am writing code for creating a list of accounts along with checkboxes when you will select the checkbox true of a particular account, it will show contact details in another table.

Controller Class:

public class AccountSelectContact{
    public List<WrapAccount> wrapAccList{get;set;}
    public List<Account> selectedAccounts{get;set;}
    public List<Contact> requiredContacts{get;set;}
    public List<Account> accList{get;set;}
    public List<Contact> conList{get;set;}
    public AccountSelectContact(){
        requiredContacts = new List<Contact>();
        if(wrapAccList== null){  
            wrapAccList= new List<WrapAccount>();
            conList = new List<Contact>();
            accList=[select id,name,(select id,name from contacts) from Account];
            for(Account a: accList){
                wrapAccList.add(new WrapAccount(a));
            }
        }
        system.debug('wrapAccList++'+wrapAccList);
    }
    public void processSelected(){
        requiredContacts = new List<Contact>();
        if(wrapAccList !=null){
            //wrapAccList= new List<WrapAccount>();
            selectedAccounts= new List<Account>();
            conList=[select id,name,AccountId from Contact where AccountId IN: accList];
            System.debug(conList);
            System.debug(wrapAccList.size());
            for(WrapAccount w:wrapAccList){
                if (w.selected==true){
                    selectedAccounts.add(w.acc);
                }
            }
            for(Account acc : selectedAccounts){
                for(Contact con : conList){
                    if(con.AccountId == acc.Id){
                        requiredContacts.add(con);
                    }
                }
            }
            System.debug(selectedAccounts);
        }
    }
    public class WrapAccount{
        public Account acc{get;set;}
        public Boolean selected{get;set;}
        public WrapAccount(Account a){
            this.acc=a;
            this.selected=false;
        }
    }
}

Visualforce Page:

<apex:page controller="AccountSelectContact">
    <apex:form >
    <apex:pageBlock title="All Accounts">     
        <apex:pageBlockButtons >
        <apex:commandButton action="{!processSelected}" value="Get Contacts" rerender="t2"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection >
        <apex:pageBlockTable value="{!wrapAccList}" var="ac">
              <apex:column >
             <apex:inputCheckbox value="{!ac.selected}"/>
              </apex:column>  
                <apex:column value="{!ac.acc.name}"/>
                <apex:column value="{!ac.acc.id}"/>
            </apex:pageBlockTable>
                <apex:pageBlockTable value="{!requiredContacts}" var="sl" id="t2">
                <apex:column value="{!sl.name}"/>
            </apex:pageBlockTable>     
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

dont miss out iconCheck out another amazing blog by Manish here: Landing Page Login Form And Save Data In Salesforce Data Extensions

Below image is showing how the scenario looks like:

Popular Salesforce Blogs