function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jaysree Ajaysree A 

Wrapper class examples

Hi,

I am very new to salesforce can any one explain the exact what is wrapper class. Please give me new example with explanation and difference betweemal Apex class and wrapper class. Please give me the example using wrapper class and without wrapper class. Please don't give me the already available examples. Thanks!
Ajay K DubediAjay K Dubedi
Hi Jaysree A,
    Wrapper is class or container  whos instance is collection of other objects. In simple words it is a custom defined type by programmer, whose structure will be defined as required by programmer. For example you may want to define a type that will conatin some account data+some images+some other custom object data. 
In order to accomodate this in a single type you cannot use any standard salesforce type [list or set or any object] you will have to define your own type and that will be wrapper class.It is construction of an object in Apex code which can combine fields from different objects or a bunch of fields which you need only in Run time to achieve your goal. 
    
You can take help from the following program. It shows only selected accounts on right side.
    
<apex:page sidebar="false" controller="WrapClass">
   
 <!--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>
 
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;
       }
  }
}


User-added image  

I hope this helps you. You can even refer to the following link:

https://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/  (https://www.minddigital.com/what-is-wrapper-class-and-how-to-use-it-in-salesforce/ )   

Regards,
Ajay
SFDC GuestSFDC Guest
Hi,

wrapper class is a custom class which has different data types or properties as per requirement. 
 In simple words, creating a new custom class with two different data types. In this example I am creating a new custom class with two data types Name, and integer.

User-added image

public class PieChartController {
    public List<PieWedgeData> getPieData() {
        List<PieWedgeData> data = new List<PieWedgeData>();
        data.add(new PieWedgeData('Apex', 50));
        data.add(new PieWedgeData('Visualforce', 30));
        data.add(new PieWedgeData('Administration', 20));
            
        return data;
    }

    // Wrapper class
    public class PieWedgeData {

        public String name { get; set; }
        public Integer data { get; set; }

        public PieWedgeData(String name, Integer data) {
            this.name = name;
            this.data = data;
        }
    }
}

Please mark it as Best answer if it is solves your problem.

Thank You,
Sohel Mohd
 
SFDC GuestSFDC Guest
Hi jaysree,

Here is visualforce page for above controller.

<apex:page controller="PieChartController" title="Pie Chart">
    <apex:chart height="350" width="450" data="{!pieData}">
        <apex:pieSeries dataField="data" labelField="name"/>
        <apex:legend position="right"/>
    </apex:chart>
</apex:page>

Please mark it as Best answer if it is solves your problem.

Thank You,
Sohel Mohd
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to know about wrapper classes.
1) http://amitsalesforce.blogspot.in/2016/03/wrapper-class-in-salesforce-select-all.html

Problem :- 
How can I display a table of records with a check box and then process only the records that are selected?

Solution:- 
Wrapper class. 

A wrapper or container class is a class, data structure, or an abstract data type whose instances are a collections of other objects.It is a custom object defined by Salesforce developer where he defines the properties of the wrapper class. Within Apex & Visualforce this can be extremely helpful to achieve many business scenarios within the Salesforce CRM software.

Using Wrapper classes we can have the ability to check few records from the displayed list and process them for some action
 
public with sharing class WrapperDemoController {
    
    public List<AccountWrapper> listAccountWrapper {get; set;}
    public List<Account> selectedAccounts{get;set;}

    public WrapperDemoController ()
    {
            listAccountWrapper = new List<AccountWrapper>();
            searchRecord();
    }
    
    public void searchRecord()
    {
        listAccountWrapper.clear();
            for(Account a: [select Id, Name,BillingState, Website, Phone ,Active__c from Account limit 10]) 
            {
                listAccountWrapper.add(new AccountWrapper(a));
            }
    }

    public void processSelected() 
    {
        selectedAccounts = new List<Account>();
        selectedAccounts.clear();
        for(AccountWrapper wrapAccountObj : listAccountWrapper) 
        {
            if(wrapAccountObj.selected == true) 
            {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }

    public void ActivateData() 
    {
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='Yes';
        }
        update selectedAccounts ;
        searchRecord();
    }

    public void DeActivateData() 
    {
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='No';
        }
        update selectedAccounts ;
        searchRecord();
    }
    


    // This is our wrapper/container class. 
    public class AccountWrapper 
    {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
        public AccountWrapper(Account a) 
        {
            acc = a;
            selected = false;
        }
    }

}
<apex:page controller="WrapperDemoController">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock id="PB1">
            <apex:pageBlockButtons >
                <apex:commandButton value="Add to Grid" action="{!processSelected}" rerender="table2,PB2"/>
            </apex:pageBlockButtons>

            <apex:pageblockSection title="All Accounts" collapsible="false" columns="1">
                <apex:pageBlockTable value="{!listAccountWrapper}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                    <apex:column value="{!accWrap.acc.Active__c}" />
                </apex:pageBlockTable>


            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageBlock id="PB2" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Activate" action="{!ActivateData}" rerender="PB1,PB2"/>
                <apex:commandButton value="DeActivate" action="{!DeActivateData}" rerender="PB1,PB2"/>
            </apex:pageBlockButtons>

                <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:column value="{!c.Active__c}" headerValue="Active"/>
                </apex:pageBlockTable>
        </apex:pageBlock>

        
    </apex:form>
</apex:page>

Let us know if this will help you

Thanks
Amit Chaudhary
jaysree Ajaysree A
Hi,

Thanks for the above replys. Can you please give the same example using without wrapper class isit possible . Let me know
farukh sk hdfarukh sk hd
This will help you for sure,
Example to display two object data using wrapper class which do not have relationship in between them,

https://www.sfdc-lightning.com/2018/10/wrapper-class-in-salesforce.html