Wrapper class in salesforce

Wrapper class in salesforce is a class which contains collection of different objects as its members, we can wrap different types of objects and can bring it on visual force page.

In the below Example, I am just wrapping the account and contact object records in “innerWrapper” wrapper class and bringing it on to the screen by iterating over declared Wrapper collection “wrapAccountList

Each account will be having more than one contact record, so we need to display contacts along with account for that i used nested apex:pageblockTable in visualforce page.

wrapper class in salesforce, salesforce tutorial, sfdcpanda, sfdc panda, wrapper class explained,

Apex Class(Wrapper class in salesforce):-

public class wrapperExample{

 //Wrapper collection declared to hold both object values
  public List<innerWrapper> wrapAccountList {get; set;}

 public wrapperExample(){
  if(wrapAccountList == null) {
  wrapAccountList = new List<innerWrapper>();
  for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {

 List<Contact> child = [select Id, Name from Contact where AccountId = :a.Id];

 wrapAccountList.add(new innerWrapper(a,child));

 // This wrapper class contains both the salesforce standard object Account and contact
  public class innerWrapper{

 public Account acc {get; set;}
  public List<Contact> con {get; set;}

 public innerWrapper(Account a, List<Contact> c) {
  acc = a;
  con = c;

Visualforce page(Wrapper class in salesforce):-

<apex:page controller="wrapperExample">
 <apex:form >
 <apex:pageBlock >
 <apex:pageblockSection title="All Accounts details and its related contacts" collapsible="true" >
 <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">

<apex:column value="{!accWrap.acc.Name}" />
 <apex:column value="{!accWrap.acc.Id}" />
 <apex:column value="{!accWrap.acc.Phone}" />

<apex:column breakBefore="false" colspan="2">
 <apex:pageBlockTable value="{!accWrap.con}" var="c">
 <apex:column value="{!c.Id}"/>
 <apex:column value="{!c.Name}"/>


Be the first to comment

Leave a Reply

Your email address will not be published.