Create visualforce page to retrieve data from multiple custom objects using custom controller wrapper class and display data in a table of VF page in Salesforce | how to fetch data from multiple different object in visualforce page

2,213 views

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]

  1.    <apex:page controller="myWrapperCustomObjCtr">
  2.   <apex:form >
  3.       <apex:pageBlock >
  4.           <apex:pageBlockSection >
  5.               <apex:pageBlockTable value="{!WrapperString}" var="wrap">
  6.                   <apex:column headerValue="CarModel">
  7.                        {!wrap.modelNameStr}
  8.                   </apex:column>
  9.  
  10.                   <apex:column headerValue="CarModel Reg Detail">
  11.                        {!wrap.carModelRegDetail}
  12.                   </apex:column>
  13.  
  14.                   <apex:column headerValue="CarMaker">
  15.                       {!wrap.makerNameStr}
  16.                   </apex:column>
  17.  
  18.                   <apex:column headerValue="Truck Name">
  19.                       {!wrap.trucNameStr}
  20.                   </apex:column>
  21.  
  22.                   <apex:column headerValue="Truck Brand">
  23.                       {!wrap.truckBrandStr}
  24.                   </apex:column>
  25.  
  26.                   <apex:column headerValue="Truck Color">
  27.                       {!wrap.truckColorStr}
  28.                   </apex:column>
  29.  
  30.                   <apex:column headerValue="Truck Co. Name">
  31.                       {!wrap.truckCompStr}
  32.                   </apex:column>
  33.  
  34.               </apex:pageBlockTable> 
  35.           </apex:pageBlockSection>
  36.       </apex:pageBlock>
  37.   </apex:form>
  38.  
  39. <br/>
  40.    <!--Start RelatedTopics Section-->
  41. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  42.  
  43.             <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>
  44.  
  45.             <br/><br/>
  46.             <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>
  47.             <div style="display:block; overflow:hidden;"> 
  48.                 <div style="width: 50%; float:left; display:inline-block">
  49.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  50.                         <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>
  51.                         <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>
  52.                         <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>
  53.                         <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>
  54.                         <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>
  55.                     </ul>
  56.             </div>
  57.  
  58.             <div style="width: 50%; float:left; display:inline-block">
  59.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  60.                         <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>
  61.                         <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>
  62.                         <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>
  63.                         <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>
  64.                         <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>
  65.                     </ul>
  66.                 </div>
  67.                <div style="clear:both;"></div> 
  68.                <br/>
  69.                 <div class="youtubeIcon">
  70.                     <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>
  71.                 </div>
  72.     </div>
  73.  
  74. </div>
  75.  
  76.   <!--End RelatedTopics Section-->
  77. </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]

  1.   public class myWrapperCustomObjCtr {
  2.  
  3.     List<carModel__c> carModalList =   NEW List<carModel__c>();
  4.     List<carMaker__c> carMakerList =   NEW List<carMaker__c>();  
  5.     List<truckModel__c> truckModelList = NEW List<truckModel__c>();
  6.  
  7.     //public wrapper getWrap {GET;SET;}
  8.     List<wrapper> stw = NEW List<wrapper>();
  9.  
  10.     public List<wrapper> getWrapperString(){
  11.  
  12.         carModalList = [SELECT Id, Name,car_model_master_rel__r.First_Name__c FROM carModel__c LIMIT 6];
  13.         carMakerList = [SELECT Id, Name FROM carMaker__c LIMIT 6];
  14.         truckModelList = [SELECT Id, Name, brand__c, color__c, company__r.Name FROM truckModel__c LIMIT 6];
  15.  
  16.         INTEGER carLength = carModalList.size();
  17.         system.debug('carModalList 1111 ' + carModalList.size());
  18.         FOR(INTEGER i=0; i < carLength; i++){
  19.             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));
  20.         }
  21.         system.debug('stw BBBCC ' + stw);
  22.         RETURN stw;
  23.  
  24.     }  
  25.  
  26.     public class wrapper{
  27.         public string modelNameStr {GET;SET;}
  28.         public string carModelRegDetail {GET;SET;}        
  29.  
  30.         public string makerNameStr {GET;SET;}
  31.  
  32.         public string trucNameStr{GET;SET;}
  33.         public string truckBrandStr{GET;SET;}
  34.         public string truckColorStr{GET;SET;}
  35.         public string truckCompStr{GET;SET;}
  36.         <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" />
  37.         public wrapper(string modelNameStr, string carModelRegDetail, string makerNameStr, string trucNameStr, string truckBrandStr, string truckColorStr, string truckCompStr){
  38.             this.modelNameStr = modelNameStr;
  39.             this.carModelRegDetail = carModelRegDetail;
  40.             this.makerNameStr = makerNameStr;
  41.  
  42.             this.trucNameStr = trucNameStr;
  43.             this.truckBrandStr = truckBrandStr;
  44.             this.truckColorStr = truckColorStr;
  45.             this.truckCompStr = truckCompStr;            
  46.  
  47.         }
  48.  
  49.     }
  50.  
  51. }

retrieve data from multiple custom objects in visualforce -- w3web.net

 

 
βœͺ The big announcement: -
As per student's demands, I re-opened my course of "Salesforce Tutorial" Absolutely Free.
I am thinking to give you free access for a powerful "Salesforce admin course" where you can Understand from Basic Concepts to advanced label in Salesforce.
πŸ‘‰ So Don't MISS it... (Access Right Now)
πŸ‘‰ Get Free Access: - Get Free Course
 

Further post that would you like to learn in Salesforce

 

 


 

FAQ (Frequently Asked Questions)

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

 
   
 
 

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment