Hey guys, today in this post we are going to learn about how to retrieve data from multiple custom objects using custom controller wrapper class and display data in a table of VF page in Salesforce.
Files we used in this post example
myWrapperClassCustomObjVfp.vfp | Visualforce Page | It is used to Create VF page to retrieve data from multiple objects and display data in table. |
customPathStatusCtrl.apxc | Apex Class Controller | It is used to retrieve data from multiple custom objects using custom controller wrapper class. |
Custom Object
carModel__c:- Name,car_model_master_rel__r.First_Name__c carMaker__c:- Name truckModel__c:- Name, brand__c, color__c, company__r.Name |
Custom Object and their fields | Fetching data from multiple different custom object in one page using custom controller |
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create Visualforce Page
Step 1:- Create Visualforce Page : myWrapperClassCustomObjVfp.vfp
From Developer Console >> File >> New >> Visualforce Page
myWrapperClassCustomObjVfp.vfp [ Visualforce Page]
<apex:page controller="myWrapperCustomObjCtr">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!WrapperString}" var="wrap">
<apex:column headerValue="CarModel">
{!wrap.modelNameStr}
</apex:column>
<apex:column headerValue="CarModel Reg Detail">
{!wrap.carModelRegDetail}
</apex:column>
<apex:column headerValue="CarMaker">
{!wrap.makerNameStr}
</apex:column>
<apex:column headerValue="Truck Name">
{!wrap.trucNameStr}
</apex:column>
<apex:column headerValue="Truck Brand">
{!wrap.truckBrandStr}
</apex:column>
<apex:column headerValue="Truck Color">
{!wrap.truckColorStr}
</apex:column>
<apex:column headerValue="Truck Co. Name">
{!wrap.truckCompStr}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</apex:page>
Create Apex Class Controller
Step 2:- Create Apex Class : myWrapperCustomObjCtr.apxc
From Developer Console >> File >> New >> Apex Class
myWrapperCustomObjCtr.apxc [Apex Class Controller]
public class myWrapperCustomObjCtr {
List<carModel__c> carModalList = NEW List<carModel__c>();
List<carMaker__c> carMakerList = NEW List<carMaker__c>();
List<truckModel__c> truckModelList = NEW List<truckModel__c>();
//public wrapper getWrap {GET;SET;}
List<wrapper> stw = NEW List<wrapper>();
public List<wrapper> getWrapperString(){
carModalList = [SELECT Id, Name,car_model_master_rel__r.First_Name__c FROM carModel__c LIMIT 6];
carMakerList = [SELECT Id, Name FROM carMaker__c LIMIT 6];
truckModelList = [SELECT Id, Name, brand__c, color__c, company__r.Name FROM truckModel__c LIMIT 6];
INTEGER carLength = carModalList.size();
system.debug('carModalList 1111 ' + carModalList.size());
FOR(INTEGER i=0; i < carLength; i++){
stw.add(NEW wrapper(carModalList[i].Name, carModalList[i].car_model_master_rel__r.First_Name__c, carMakerList[i].Name, truckModelList[i].Name,truckModelList[i].brand__c,truckModelList[i].color__c,truckModelList[i].company__r.Name));
}
system.debug('stw BBBCC ' + stw);
RETURN stw;
}
public class wrapper{
public string modelNameStr {GET;SET;}
public string carModelRegDetail {GET;SET;}
public string makerNameStr {GET;SET;}
public string trucNameStr{GET;SET;}
public string truckBrandStr{GET;SET;}
public string truckColorStr{GET;SET;}
public string truckCompStr{GET;SET;}
<img src="https://www.w3web.net/wp-content/uploads/2021/05/vfpMultipleObjectsImg-300x165.png" alt="" width="300" height="165" class="alignnone size-medium wp-image-4536" />
public wrapper(string modelNameStr, string carModelRegDetail, string makerNameStr, string trucNameStr, string truckBrandStr, string truckColorStr, string truckCompStr){
this.modelNameStr = modelNameStr;
this.carModelRegDetail = carModelRegDetail;
this.makerNameStr = makerNameStr;
this.trucNameStr = trucNameStr;
this.truckBrandStr = truckBrandStr;
this.truckColorStr = truckColorStr;
this.truckCompStr = truckCompStr;
}
}
}
Further post that would you like to learn in Salesforce
What is retrieve in Salesforce?
Use the retrieve() call to retrieve individual records from an object. The client application passes the list of fields to retrieve, the object, and an array of record IDs to retrieve. The retrieve() call does not return records that have been deleted.
How do I recover a deleted record in Salesforce?
Login to Workbench using your Salesforce credentials and select the queries drop-down | SOQL Query. Select the object that contains the records to be restored. Select Include under List and for Deleted and archived records.
What are retrieve and deploy in Salesforce?
Use the deploy() and retrieve() calls to move metadata (XML files) between a Salesforce organization and a local file system. At any time you can deploy those changes to another Salesforce organization.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |