+ Start a Discussion
ANKITAANKITA 

Reg: Wrapper Classes in Apex Programming.

Hi,

 

I am very new to salesforce. I Observed most of the application are designed by wrapper classes in my Organization.

Can you please share some real time code samples regarding to wrapper class.

Please tell me where we use wrapper class exactly?

It  helps to understand wrapper classes easily. Your help will be greatly helps to me.

 

Thanks

ANKITA

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

A small but indeed a very helpful link by salesforce :

 

http://wiki.developerforce.com/index.php/Wrapper_Class

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

Shashikant SharmaShashikant Sharma

Hi Ankita,

 

Wrapper class is some kind of Apex Class Object Type. There are situations when we need to create a list of items which are of various type.

In Apex List can only contain a single type of items like List<Account> , List<Contact> , if I want to have both items then I would create a wrapper class like

 

Basic Structure

 

public class outerClass {
public List<innerWrapperClass> wrapperList {get;set;}

public outerClass()
{
      wrapperList .add(new innerWrapperClass(new Account(Name ='Acc') , new Contact(lastName = 'Con'));
}

public class innerWrapperClass{
public Account a{get;set;}
public Contact c{get;set;}

public innerClass(Account a , Contact c)
{
    this.a = a;
    this.c = c;
}

}
}

 

 Example : When ever you want to show a check box in a table of any list of items you will require wrapper class, like in this example we will show some record on page 1 and user will select some and will click on nextpage button and on page2 we will show only selected records

 

Apex Class :

 

public class demoClass1 {

public demoClass1()
    {
    
        listContactWrapper  = new  List<contactWrapper>(); 
        for(Contact c : [Select Name , Email from Contact limit 20])
            listContactWrapper.add(new contactWrapper(c));
    }


public PageReference previouspage()
    {
        addTextrBox c = new addTextrBox();
        //c.addTextrBox1();
        return new PageReference(Page.page1.getUrl());
        
        
    }
public PageReference nextpage()
    {
        return new PageReference(Page.page2.getUrl());
    }
    
public List<contactWrapper> listContactWrapper {get;set;}
public class contactWrapper
   {
      public Contact c {get;set;}
      public Boolean isChecked {get;set;}
      public contactWrapper(Contact c)
          {
              this.c = c;
              if(isChecked == null)
                  isChecked = false;
              
          } 
   }

}

 

 

Page1

<apex:page controller="demoClass1">
    <apex:form >
    
    
     <apex:pageBlock id="PageBlockId" title="Test Wrapper Class">
         
         <apex:pageBlockButtons >
             <apex:commandButton value="Next Page" action="{!nextpage}" onclick="return openWindow();"/>
         </apex:pageBlockButtons>
         <apex:pageBlockSection >
         
             <apex:pageBlockTable value="{!listContactWrapper}" var="item">
                 
                 <apex:column >
                     <apex:inputCheckbox value="{!item.isChecked}"/>
                 </apex:column>
             
                  <apex:column value="{!item.c.Name}"/>
                  <apex:column value="{!item.c.Email}"/> 
             </apex:pageBlockTable>
             
         </apex:pageBlockSection>
     
     </apex:pageBlock>
     </apex:form>
</apex:page>
 

 

 

Page 2

<apex:page controller="demoClass1" >
    <apex:form onmouseout="closeWindow();">
    
     <apex:pageBlock >
         <apex:pageBlockButtons >
             <apex:commandButton value="Previous Page" action="{!previouspage}"/>
         </apex:pageBlockButtons>
         <apex:pageBlockSection >
         
             <apex:pageBlockTable value="{!listContactWrapper}" var="item">
                  <apex:column value="{!item.c.Name}" rendered="{!item.isChecked}"/>
                  <apex:column value="{!item.c.Email}" rendered="{!item.isChecked}"/> 
             </apex:pageBlockTable>
             
         </apex:pageBlockSection>
     
     </apex:pageBlock>
     </apex:form>
</apex:page>
              

 

 

I hope above example will help you.

Ankit AroraAnkit Arora

A small but indeed a very helpful link by salesforce :

 

http://wiki.developerforce.com/index.php/Wrapper_Class

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
ANKITAANKITA

Its too good!

Thanku so much.

 

ANKITAANKITA

Hi,

I am very thankful to both SHASHI and ANKIT.

I worked on these samples very well. It works fine.

Could you please share some more real time samples.

It helps to all the newly entered people and me.

 

Thanks,

ANKITA

philanthropyphilanthropy

Shashi, how would you write a test class for your  demoClass1 example? I'm having a hard time writing a test class for my wrapper class based on the example Ankit provided and seeing a test example for you demo would be very helpful.

 

Thanks!

Shashikant SharmaShashikant Sharma

Just follow these steps 

 

http://forceschool.blogspot.com/2011/06/testing-apex-structure-of-test-class.html

 

let me knoww if any issues in it.

Devendra@SFDCDevendra@SFDC

 

 

Hi,

 

How to use radio buttons instead of checkboxes using Wrapper classes??

 

Thanks and Regards,

Devendra S

lakshman.mattilakshman.matti

some events u mentioned in the code are for what purpose..like event in command button and one more event in <Apex:form> component


Shashikant Sharma wrote:

Hi Ankita,

 

Wrapper class is some kind of Apex Class Object Type. There are situations when we need to create a list of items which are of various type.

In Apex List can only contain a single type of items like List<Account> , List<Contact> , if I want to have both items then I would create a wrapper class like

 

Basic Structure

 

public class outerClass {
public List<innerWrapperClass> wrapperList {get;set;}

public outerClass()
{
      wrapperList .add(new innerWrapperClass(new Account(Name ='Acc') , new Contact(lastName = 'Con'));
}

public class innerWrapperClass{
public Account a{get;set;}
public Contact c{get;set;}

public innerClass(Account a , Contact c)
{
    this.a = a;
    this.c = c;
}

}
}

 

 Example : When ever you want to show a check box in a table of any list of items you will require wrapper class, like in this example we will show some record on page 1 and user will select some and will click on nextpage button and on page2 we will show only selected records

 

Apex Class :

 

public class demoClass1 {

public demoClass1()
    {
    
        listContactWrapper  = new  List<contactWrapper>(); 
        for(Contact c : [Select Name , Email from Contact limit 20])
            listContactWrapper.add(new contactWrapper(c));
    }


public PageReference previouspage()
    {
        addTextrBox c = new addTextrBox();
        //c.addTextrBox1();
        return new PageReference(Page.page1.getUrl());
        
        
    }
public PageReference nextpage()
    {
        return new PageReference(Page.page2.getUrl());
    }
    
public List<contactWrapper> listContactWrapper {get;set;}
public class contactWrapper
   {
      public Contact c {get;set;}
      public Boolean isChecked {get;set;}
      public contactWrapper(Contact c)
          {
              this.c = c;
              if(isChecked == null)
                  isChecked = false;
              
          } 
   }

}

 

 

Page1

<apex:page controller="demoClass1">
    <apex:form >
    
    
     <apex:pageBlock id="PageBlockId" title="Test Wrapper Class">
         
         <apex:pageBlockButtons >
             <apex:commandButton value="Next Page" action="{!nextpage}" onclick="return openWindow();"/>
         </apex:pageBlockButtons>
         <apex:pageBlockSection >
         
             <apex:pageBlockTable value="{!listContactWrapper}" var="item">
                 
                 <apex:column >
                     <apex:inputCheckbox value="{!item.isChecked}"/>
                 </apex:column>
             
                  <apex:column value="{!item.c.Name}"/>
                  <apex:column value="{!item.c.Email}"/> 
             </apex:pageBlockTable>
             
         </apex:pageBlockSection>
     
     </apex:pageBlock>
     </apex:form>
</apex:page>
 

 

 

Page 2

<apex:page controller="demoClass1" >
    <apex:form onmouseout="closeWindow();">
    
     <apex:pageBlock >
         <apex:pageBlockButtons >
             <apex:commandButton value="Previous Page" action="{!previouspage}"/>
         </apex:pageBlockButtons>
         <apex:pageBlockSection >
         
             <apex:pageBlockTable value="{!listContactWrapper}" var="item">
                  <apex:column value="{!item.c.Name}" rendered="{!item.isChecked}"/>
                  <apex:column value="{!item.c.Email}" rendered="{!item.isChecked}"/> 
             </apex:pageBlockTable>
             
         </apex:pageBlockSection>
     
     </apex:pageBlock>
     </apex:form>
</apex:page>
              

 

 

I hope above example will help you.