Wrapper Class #inSalesforce

Wrapper Class in Salesforce

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

Example: This is to display records from two or more objects (not related to each other) in a single table

Account and Candidate__c object here are not related to each other and you want to display the date together in one page


CLASS:

public class sampleWrapperClass {
public list<wrapperClass> wrapList{get;set;}
public list<wrapperClass> getwrapperAccount(){
list<Account> aList = [select Id, Name from Account limit 10];
wrapList = new list<wrapperClass>();
for(Account acct: aList) {
wrapList.add(new wrapperClass(acct)); 
}
return wrapList;
}
public list<wrapperClass> getwrapperCandidate(){
list<Candidate__c>cndList=[select Id, Name from Candidate__c limit 10];
wrapList= new list<wrapperClass>();
for(Candidate__c cd : cndList ){
wrapList.add(new wrapperClass(cd));
}
return wrapList;
}
public class wrapperClass{
public Account acc {get;set;}
public Candidate__c c {get;set;}
public wrapperClass(Account accn){
acc = accn;        
}
public wrapperClass(Candidate__c cd){
c = cd;
}
}
}


PAGE:

<apex:page controller="sampleWrapperClass">
<apex:pageBlock title="Account From wrapper  Class">
<apex:pageBlockTable value="{!wrapperAccount}" var="w">
<apex:column value="{!w.acc.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Wrapper Class">
<apex:pageBlockTable value="{!wrapperCandidate}" var="wc">
<apex:column value="{!wc.c.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>



Comments