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
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]
|
<aura:component controller="relationshipQueriesCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="carRelatedList" type="carMaker__c[]"/>
<aura:attribute name="carAryList" type="carModel__c[]"/>
<div class="slds">
<strong>Parent to Child Soql Query on Custom object in Salesforce</strong><br/>
<div class="scrollable">
<table class="slds-table slds-table_bordered slds-table_cell-buffer">
<thead>
<tr class="slds-text-title_caps">
<th>Car Maker</th>
<th>Car Model</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.carRelatedList}" var="carItem">
<tr>
<td>{!carItem.Name}</td>
<td>
<ul>
<aura:iteration items="{!carItem.carModels1__r}" var="carModel">
<li>
<a data-recId="{!carItem.Id}" href="" onclick="{!c.navigate}">
{!carModel.Name}
</a>
</li>
</aura:iteration>
</ul>
</td>
</tr>
</aura:iteration>
</tbody>
</table>
<br/><br/>
</div>
<br/><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-->
</div>
</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]
|
({
doInit : function(component, event, helper) {
helper.relatedListHelper(component);
},
navigate:function(component, event, helper){
console.log('=====');
var record = event.currentTarget.getAttribute("data-recId");
//var record = event.getSource().get("v.id");
console.log('====='+record);
var sObectEvent = $A.get("e.force:navigateToSObject");
sObectEvent.setParams({
"recordId": record
});
sObectEvent.fire();
},
})
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]
|
({
relatedListHelper : function(component,carBrand) {
var action = component.get('c.getRelatedList');
action.setParams({
"recId" : component.get('v.recordId'),
});
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var results = response.getReturnValue();
//alert('results ' + JSON.stringify(results));
component.set("v.carRelatedList",results);
}
});
$A.enqueueAction(action);
},
})
Create Apex Class Controller
Step 4:- Create Apex Class : relationshipQueriesCtrl.apxc
From Developer Console >> File >> New >> Apex Class
relationshipQueriesCtrl.apxc [Apex Class Controller]
|
public class relationshipQueriesCtrl {
@AuraEnabled
public static List<carMaker__c> getRelatedList(String recId){
List<carMaker__c> getcarMakerList = [SELECT Id, Name, (SELECT Id, Name FROM carModels1__r ) FROM carMaker__c WHERE Id=:recId];
RETURN getcarMakerList;
}
}
Further post that would you like to learn in Salesforce
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 |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |