Activity Forums Salesforce® Discussions What are wrapper class? How to use when working with different objects on Visualforce page?

  • PRANAV

    Member
    March 26, 2018 at 1:15 pm

    Hi Ankit,

    A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects.

    In Apex and Visualforce this type of class can be extremely useful to achieve lot of business scenario.

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

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

    In the Visualforce community boards one of the most commonly asked questions is, "How can I display a table of records with a check box and then process only the records that are selected?" This is a perfect scenario where a wrapper class can be used.

    For more you can search out "Wrapper Class" in the Salesforce Technical Library Documentation.

    Hope this helps you.

  • Archit

    Member
    March 26, 2018 at 2:32 pm

    Hello Ankit,

    A wrapper class 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. Similarly wrapper class is a custom class which has different data types or properties as per requirement. We can wrap different objects types or any other types in a wrapper class.

    Thanks!

  • Neha

    Member
    March 27, 2018 at 3:56 am

    Hi Ankit,

    You can understand Wrapper like this.If you want to make an object which includes both( account and contact) or( account and its corresponding selection checkbox) you use the wrapper class

    -I am giving code. only selected accounts are shown on the right side.

    <apex:page sidebar="false" controller="WrapClass"></apex:page>

    <!--VF PAGE BLOCK-->

    <apex:form>
    <apex:pageblock>
    <apex:pageblockbuttons>
    <apex:commandbutton action="{!ProcessSelected}" value="Show Selected accounts" rerender="block2"></apex:commandbutton>
    </apex:pageblockbuttons>
    <apex:pageblocksection columns="2">
    <apex:pageblocktable value="{!wrapaccountList}" var="waccl"></apex:pageblocktable></apex:pageblocksection></apex:pageblock></apex:form>

    <apex:column>
    <apex:facet name="header">
    <apex:inputcheckbox></apex:inputcheckbox>
    </apex:facet>
    <apex:inputcheckbox value="{!waccl.isSelected}" id="InputId"></apex:inputcheckbox>
    </apex:column>

    <apex:column value="{!waccl.accn.name}"></apex:column>
    <apex:column value="{!waccl.accn.phone}"></apex:column>
    <apex:column value="{!waccl.accn.billingcity}"></apex:column>

    <apex:pageblocktable value="{!selectedAccounts}" var="sa" id="block2">
    <apex:column value="{!sa.name}"></apex:column>
    <apex:column value="{!sa.phone}"></apex:column>
    <apex:column value="{!sa.billingcity}"></apex:column>
    </apex:pageblocktable>

    public class WrapClass {

    //CONTROLLER CLASS

    public list<wrapaccount> wrapaccountList { get; set; }
    public list<account> selectedAccounts{get;set;}
    public WrapClass (){

    //if(wrapaccountList ==null){
    wrapaccountList =new list<wrapaccount>();
    for(account a:[select id,name,billingcity,phone from account limit 10]){
    wrapaccountlist.add(new wrapaccount(a));

    }
    // }
    }

    //### SELECTED ACCOUNT SHOWN BY THIS METHOD
    public void ProcessSelected(){
    selectedAccounts=new list<account>();

    for(wrapaccount wrapobj:wrapaccountlist){
    if(wrapobj.isSelected==true){
    selectedAccounts.add(wrapobj.accn);
    }

    }
    }

    //##THIS IS WRAPPER CLASS
    // account and checkbox taken in wrapper class

    public class wrapaccount{

    public account accn{get;set;}
    public boolean isSelected{get;set;}

    public wrapaccount(account a){

    accn=a;
    isselected=false;
    }
    }
    }

    Hope this example gives you a better understanding of Wrapper Class and its use.

    Happy Salesforce 🙂

    • This reply was modified 6 years, 9 months ago by  Neha.
  • Parul

    Member
    September 20, 2018 at 7:22 pm

    Hi,

    A wrapper or container class is a class which contains different objects or collection of objects as its members.

    A wrapper class 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. Similarly wrapper class is a custom class which has different data types or properties as per requirement. We can wrap different objects types or any other types in a wrapper class.

    In the Visualforce most important use case is to display a table of records with a check box and then process only the records that are selected.

  • shariq

    Member
    September 20, 2018 at 9:48 pm

    Hi,

    You can understand Wrapper like this.If you want to make a object which includes both( account and contact) or( account and its corresponding selection check box) you use wrapper class

     

    <apex:page sidebar="false" controller="WrapTest ">

    <!--VF PAGE BLOCK-->

    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockButtons >
    <apex:commandButton action="{!ProcessSelected}" value="Show Selected accounts" reRender="block2"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection columns="2">
    <apex:pageBlockTable value="{!wrapaccountList}" var="waccl">

    <apex:column >
    <apex:facet name="header">
    <apex:inputCheckbox />
    </apex:facet>
    <apex:inputCheckbox value="{!waccl.isSelected}" id="InputId"/>
    </apex:column>

    <apex:column value="{!waccl.accn.name}"/>
    <apex:column value="{!waccl.accn.phone}"/>
    <apex:column value="{!waccl.accn.billingcity}"/>
    </apex:pageBlockTable>

    <apex:pageBlockTable value="{!selectedAccounts}" var="sa" id="block2">
    <apex:column value="{!sa.name}"/>
    <apex:column value="{!sa.phone}"/>
    <apex:column value="{!sa.billingcity}"/>
    </apex:pageBlockTable>

    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Controller -

    public class WrapTest {

    //CONTROLLER CLASS

    public list<wrapaccount> wrapaccountList { get; set; }
    public list<account> selectedAccounts{get;set;}

    public WrapTest (){

    //if(wrapaccountList ==null){
    wrapaccountList =new list<wrapaccount>();
    for(account a:[select id,name,billingcity,phone from account limit 10]){
    wrapaccountlist.add(new wrapaccount(a));

    }
    // }
    }

    //### SELECTED ACCOUNT SHOWN BY THIS METHOD
    public void ProcessSelected(){
    selectedAccounts=new list<account>();

    for(wrapaccount wrapobj:wrapaccountlist){
    if(wrapobj.isSelected==true){
    selectedAccounts.add(wrapobj.accn);
    }

    }
    }

    //##THIS IS WRAPPER CLASS
    // account and checkbox taken in wrapper class

    public class wrapaccount{

    public account accn{get;set;}
    public boolean isSelected{get;set;}

    public wrapaccount(account a){

    accn=a;
    isselected=false;
    }
    }
    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs