How to write a Cross-object (Nested Queries) SOQL query from Parent to child, Retrieve the related list values and display on Lightning Component in Salesforce | how to access child records from parent in salesforce

1,227 views

Hey guys, today in this post we are going to learn about How to write a Cross-object (Nested Queries) SOQL query from Parent to child, Retrieve the related list data and display on Lightning Component in Salesforce

Files we used in this post example

relationshipQueriesCmp.cmp Lightning Component It is used for create a table for display the Related List Values
relationshipQueriesCmpController.js JavaScript Controller File It is hold Javascript doInit functionality.
relationshipQueriesCmpHelper.js JavaScript Controller Helper File It is hold for Javascript Helper Function to get Nested Queries Values from apex method
relationshipQueriesCtrl.apxc Apex Class Controller It is used for get SOQL query from Parent to child and Retrieve the related list data from Apex Method

Live Demo

cross object nested soql queries in salesforce -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

 

Create Lightning Component

Step 1:- Create Lightning Component : relationshipQueriesCmp.cmp

From Developer Console >> File >> New >> Lightning Component

relationshipQueriesCmp.cmp [Lightning Component File]

  1.   <aura:component controller="relationshipQueriesCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
  2.     <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
  3.     <aura:attribute name="carRelatedList" type="carMaker__c[]"/>
  4.     <aura:attribute name="carAryList" type="carModel__c[]"/>
  5.  
  6.     <div class="slds">
  7.         <strong>Parent to Child Soql Query on Custom object in Salesforce</strong><br/>
  8.  
  9.         <div class="scrollable">
  10.             <table class="slds-table slds-table_bordered slds-table_cell-buffer">                
  11.                 <thead>
  12.                     <tr class="slds-text-title_caps">
  13.                         <th>Car Maker</th>
  14.                         <th>Car Model</th>                        
  15.                     </tr>
  16.                 </thead>     
  17.                 <tbody>
  18.  
  19.                     <aura:iteration items="{!v.carRelatedList}" var="carItem">                        
  20.                         <tr> 
  21.                             <td>{!carItem.Name}</td>
  22.                             <td> 
  23.                                <ul> 
  24.                                 <aura:iteration items="{!carItem.carModels1__r}" var="carModel"> 
  25.                                    <li> 
  26.                                         <a data-recId="{!carItem.Id}" href="" onclick="{!c.navigate}">
  27.                                             {!carModel.Name}
  28.                                         </a>
  29.                                    </li>    
  30.                                 </aura:iteration> 
  31.                                </ul>    
  32.                             </td>      
  33.                         </tr> 
  34.                     </aura:iteration>    
  35.                 </tbody>
  36.             </table>
  37.  
  38.             <br/><br/>
  39.  
  40.         </div>
  41.  
  42.  <br/><br/>
  43.    <!--Start RelatedTopics Section-->
  44. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  45.  
  46.             <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>
  47.  
  48.             <br/><br/>
  49.             <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>
  50.             <div style="display:block; overflow:hidden;"> 
  51.                 <div style="width: 50%; float:left; display:inline-block">
  52.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  53.                         <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>
  54.                         <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>
  55.                         <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>
  56.                         <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>
  57.                         <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>
  58.                     </ul>
  59.             </div>
  60.  
  61.             <div style="width: 50%; float:left; display:inline-block">
  62.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  63.                         <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>
  64.                         <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>
  65.                         <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>
  66.                         <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>
  67.                         <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>
  68.                     </ul>
  69.                 </div>
  70.                <div style="clear:both;"></div> 
  71.                <br/>
  72.                 <div class="youtubeIcon">
  73.                     <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>
  74.                 </div>
  75.     </div>
  76.  
  77. </div>
  78.  
  79.   <!--End RelatedTopics Section-->
  80.  
  81.     </div>
  82. </aura:component>

Create Component JavaScript Controller

Step 2:- Create Lightning Component : relationshipQueriesCmpController.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller

relationshipQueriesCmpController.js [JavaScript Controller]

  1.   ({
  2. 	doInit : function(component, event, helper) {  
  3.        helper.relatedListHelper(component);
  4.     },
  5.  
  6.     navigate:function(component, event, helper){
  7.         console.log('=====');
  8.         var record = event.currentTarget.getAttribute("data-recId");           
  9.         //var record = event.getSource().get("v.id");
  10.         console.log('====='+record);
  11.         var sObectEvent = $A.get("e.force:navigateToSObject");          
  12.         sObectEvent.setParams({
  13.             "recordId":  record           
  14.         });
  15.         sObectEvent.fire(); 
  16.     },
  17.  
  18.  
  19. })

Create Component JavaScript Helper

Step 3:- Create Lightning Component : relationshipQueriesCmpHelper.js

From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper

relationshipQueriesCmpHelper.js [JavaScript Helper File]

  1.   ({
  2. 	 relatedListHelper : function(component,carBrand) {
  3.         var action = component.get('c.getRelatedList');        
  4.  
  5.         action.setParams({
  6.             "recId" : component.get('v.recordId'),                
  7.         });
  8.  
  9.         action.setCallback(this, function(response) {
  10.             var state = response.getState();
  11.             if (state === "SUCCESS") {      
  12.                 var results = response.getReturnValue();
  13.                 //alert('results ' + JSON.stringify(results));
  14.                 component.set("v.carRelatedList",results);                  
  15.             }
  16.         }); 
  17.  
  18.         $A.enqueueAction(action);
  19.     },
  20.  
  21. })

Create Apex Class Controller

Step 4:- Create Apex Class : relationshipQueriesCtrl.apxc

From Developer Console >> File >> New >> Apex Class

relationshipQueriesCtrl.apxc [Apex Class Controller]

  1.   public class relationshipQueriesCtrl {
  2.  
  3.     @AuraEnabled
  4.     public static List<carMaker__c> getRelatedList(String recId){
  5.         List<carMaker__c> getcarMakerList = [SELECT Id, Name, (SELECT Id, Name FROM carModels1__r ) FROM carMaker__c WHERE Id=:recId];
  6.         RETURN getcarMakerList;
  7.     }
  8.  
  9. }

cross object nested soql queries in salesforce -- w3web.net

Further post that would you like to learn in Salesforce

 


 

FAQ (Frequently Asked Questions)

How do I write a nested SOQL query?

Nested queries—also known as a left outer join relationship—are a top-down approach to creating a SOQL query. A classic example of this would be the standard Account object with many child objects that are unrelated to one another: Opportunities, Contacts, Cases, and Attachments.

How many SOQL nested queries?

There is a limit of 4 External objects that can be joined in SOQL. Joins have a huge performance impact on data processing. You can expect longer response times for each join in a query.

What is inner query in SOQL?

You need the relationship name to perform inner SOQL query. For example, if you are going to perform a query to get Accounts along with their contacts then your query will be something like - Select Id, Name,(Select Id, LastName from Contacts) from Account.

Related Topics | You May Also Like

  • Our Free Courses →
  • 👉 Get Free Course →

    📌 Salesforce Administrators

    📌 Salesforce Lightning Flow Builder

    📌 Salesforce Record Trigger Flow Builder

 
 


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