Any Salesforce developer would be know with creating parent child relationship queries in APEX. For example getting a list of Contacts with their respective Account we use such queries. However when we take that data to Lightning component/Visualforce pages it becomes a little bit difficult to show that data. This problem can be solved with the help of wrapper class.
Wrapper is like create a custom object with multitype of data like list of string, list of object integer, decimal etc.
We create a class in class so we called this Wrapper class. Learn with simple example.
AccountContactWrapper.cla :
public class AccountContactWrapper{
public List<WrapperClass> wrapAccountList {get; set;}
public AccountContactWrapper(){
fetchAccountContactData();
}
public void fetchAccountContactData(){
wrapAccountList = new List<WrapperClass>();
for(Account acc : [SELECT Id, Name, (SELECT Id, Name, Email FROM Contacts) FROM Account LIMIT 100]){
wrapAccountList.add(new WrapperClass(acc,acc.Contacts, acc.Contacts.SIZE()));
}
}
// This wrapper class
public class WrapperClass{
public Account acc {get; set;}
public List<Contact> conList {get; set;}
public Integer contactSize{get;set;}
public Boolean isCheck{get;set;}
public WrapperClass(Account a, List<Contact> cont, Integer countContact) {
this.acc = a;
this.conList = cont;
this.contactSize = countContact;
this.isCheck = false;
}
}
}
Visyalforce Page : Here we create a vf page to show the wrapper data
AccountWrapper.vfp :
<apex:page controller="AccountContactWrapper" action="{!fetchAccountContactData}">
<apex:slds />
<apex:form >
<table class="slds-table slds-table_cell-buffer slds-table_bordered" aria-label="Example default base table of Opportunities">
<thead>
<tr class="slds-line-height_reset">
<th class="" scope="col">
<div class="slds-truncate" title="Stage">Is Check</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Account Name">Account Name</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Close Date">Contacts Size</div>
</th>
</tr>
</thead>
<tbody>
<apex:variable var="rowNum" value="{!1}"/>
<apex:repeat value="{!wrapAccountList}" var="wrap">
<tr class="slds-hint-parent">
<td>
<apex:inputCheckbox value="{!wrap.isCheck}" />
</td>
<td data-label="Account Name">
<div class="slds-truncate" title="Cloudhub">
<a href="/{!wrap.acc.Id}" tabindex="-1">{!wrap.acc.Name}</a>
</div>
</td>
<td data-label="Close Date">
<div class="slds-truncate" title="4/14/2015">{!wrap.contactSize}</div>
</td>
</tr>
<apex:variable var="rowNum" value="{!rowNum + 1}"/>
</apex:repeat>
</tbody>
</table>
</apex:form>
</apex:page>
4 comments
Its like you learn my thoughts! You seem to grasp
a lot about this, like you wrote the e book in it or something.
I feel that you can do with a few percent to
drive the message home a bit, however other than that,
that is great blog. A fantastic read. I’ll definitely be back.
Thanks @THERON
Looking forward to reading more. Great blog. Really Great.
Thanks @DAMIAN