Hey guys, today in this post we are going to learn about How to write SOQL Query to Retrieve/Search All Account Contact Relationship records and display account related contacts based on recordId on click button in Lightning Component Salesforce.
Client applications need to be able to query for more than a single type of object at a time. SOQL provides syntax to support these types of queries, called relationship queries, against standard objects and custom objects. Relationship queries traverse parent-to-child and child-to-parent relationships between objects to filter and return results.
Relationship queries are similar to SQL joins. However, you cannot perform arbitrary SQL joins. The relationship queries in SOQL must traverse a valid relationship path as defined in the rest of this section. To know more details about Relationship Queries, Click Here..
Files we used to display Relationship Queries in lightning component â
AccountCmp.cmp | Lightning Component | It is used to display Relationship Queries in lightning component |
AccountCmpController.js | JavaScript Controller | It is hold Javascript doInit function to fetch and display list of records. |
AccountCmpApp.app | Lightning Application | It is used to call the component and preview on browser. |
accCtrl.apxc | Apex Class Controller | It is used for get SOQL query to retrieve Account records. |
Final Output â
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps âž
Create Lightning Component
Step 1:- Create Lightning Component : AccountCmp.cmp
From Developer Console >> File >> New >> Lightning Component
AccountCmp.cmp [Lightning Component File]
![]() |
<aura:component controller="accCtrl" 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="accItemList" type="List"/>
<aura:attribute name="srchList" type="List"/>
<aura:attribute name="conItemList" type="List"/>
<aura:attribute name="modalFade" type="boolean" default="false"/>
<div class="slds slds-p-around_medium">
<div class="slds-p-bottom_medium"><ui:inputText class="slds-input" aura:id="srhId" keyup="{!c.searchName}" updateOn="keyup" placeholder="Search Name.."/> </div>
<table class="slds-table slds-table--bordered slds-table--col-bordered" style="border-collapse:collapse; border:1px #ddd solid;">
<thead>
<tr style="background:#ddd;">
<th style="background:#eee;">Name</th>
<th style="background:#eee;">Phone</th>
<th style="background:#eee;">Industry</th>
<th style="background:#eee;"></th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.accItemList}" var="obj" indexVar="index">
<tr>
<td>{!obj.Name}</td>
<td>{!obj.Phone}</td>
<td>{!obj.Industry}</td>
<td><button class="slds-button slds-button_brand" id="{!obj.Id}" onclick="{!c.contactAction}" >Related Contacts</button></td>
</tr>
</aura:iteration>
</tbody>
</table>
<aura:if isTrue="{!v.modalFade}">
<section id="modalFade" class="slds-modal slds-fade-in-open" >
<div class="slds-modal__container">
<header class="slds-modal__header">
<button class="slds-button slds-modal__close slds-button--icon-inverse" name="dataRowIcon0" title="Close" onclick="{!c.closePopup}">
<lightning:icon iconName="utility:close" variant="bare" icon-class="slds-m-around_medium" size="small" alternativeText="close"/>
</button>
<h2 class="slds-text-heading_medium slds-hyphenate">Related Contacts</h2>
</header>
<div class="slds-modal__content slds-p-around_medium">
<table class="slds-table slds-table--bordered slds-table--col-bordered" style="border-collapse:collapse; border:1px #ddd solid;">
<thead>
<tr style="background:#ddd;">
<th style="background:#eee;">First Name</th>
<th style="background:#eee;">Last Name</th>
<th style="background:#eee;">Phone</th>
</tr>
</thead>
<tbody>
<aura:iteration items="{!v.conItemList}" var="acItem" indexVar="index">
<aura:iteration items="{!acItem.Contacts}" var="conItem">
<tr>
<td>{!conItem.FirstName}</td>
<td>{!conItem.LastName}</td>
<td>{!conItem.Phone}</td>
</tr>
</aura:iteration>
</aura:iteration>
</tbody>
</table>
</div>
<footer class="slds-modal__footer">
<button class="slds-button slds-button--destructive" name="dataRowIcon0" onclick="{!c.closePopup}">Cancel</button>
</footer>
</div>
</section>
<div id="modalBackdrop" class="slds-backdrop slds-backdrop_open"></div>
</aura:if>
</div>
</aura:component>
Create JavaScript Controller
Step 2:- Create Lightning Component : AccountCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
AccountCmpController.js [JavaScript Controller]
![]() |
({
doInit : function(component,event,helper) {
var action = component.get("c.accListData");
action.setCallback(this,function(response){
var state = response.getState();
//alert('state' + state);
if(state=='SUCCESS'){
var result = response.getReturnValue();
component.set("v.accItemList",result);
}
});
$A.enqueueAction(action);
},
searchName:function(component,event,helper){
var srhId = component.find('srhId').get('v.value');
//alert('srhId ' + srhId);
var action = component.get("c.srchAccName");
action.setParams({"searchKey":srhId});
action.setCallback(this,function(response){
var state = response.getState();
//alert('state' + state);
if(state=='SUCCESS'){
var result = response.getReturnValue();
component.set("v.accItemList",result);
}
});
$A.enqueueAction(action);
},
contactAction:function(component,event,helper){
var thisObj = event.target.id;
//alert('srhId ' + srhId);
var action = component.get("c.AccountNested");
action.setParams({"recId":thisObj});
action.setCallback(this,function(response){
var state = response.getState();
//alert('state' + state);
if(state=='SUCCESS'){
var result = response.getReturnValue();
//alert(JSON.stringify(result));
component.set("v.conItemList",result);
component.set("v.modalFade",true);
}
});
$A.enqueueAction(action);
},
closePopup:function(component,event,helper){
component.set("v.modalFade",false);
}
})
Create Apex Class Controller
Step 3:- Create Apex Class : accCtrl.apxc
From Developer Console >> File >> New >> Apex Class
accCtrl.apxc [Apex Class Controller]
![]() |
public class accCtrl {
@AuraEnabled
public static List<Account> AccountNested(String recId){
List<Account> accNestObj = [SELECT Id, Name, Phone, Industry, (SELECT Id, FirstName, LastName, Phone, Email FROM Contacts) FROM Account WHERE Id=:recId];
RETURN accNestObj;
}
@AuraEnabled
public static List<Account> accListData() {
List<Account> accList = [SELECT Id, Name, Phone, Industry FROM Account WHERE Industry !=NULL];
RETURN accList;
}
@AuraEnabled
public static List<Account> srchAccName(String searchKey){
searchKey = '%' + searchKey + '%';
List<Account> accList = [SELECT Id, Name, Phone, Industry FROM Account WHERE Name LIKE:searchKey];
RETURN accList;
}
}
Create Lightning Application
Step 4:- Create Lightning Application : AccountCmpApp.app
From Developer Console >> File >> New >> Lightning Application
AccountCmpApp.app [Component Application File]
![]() |
<aura:application extends="force:slds" >
<c:accountCmp/>
</aura:application>
Further post that would you like to learn in Salesforce
How do I find my account hierarchy in Salesforce?
From Setup, in the Quick Find box, enter Account Settings and then click Account Settings. Select Show View Hierarchy link on account pages in Salesforce Classic. Whenever an account is related to another account via the Parent Account field, the account detail page includes a View Hierarchy link.
How do I use account hierarchy in Salesforce lightning?
You can edit the hierarchy columns to show the information that's most useful to your sales reps. From Setup, at the top of the page, select Object Manager. In Account, click Hierarchy Columns and then edit the columns.
How do I find my account hierarchy?
Account hierarchy is available in: Group, Professional, Enterprise, Performance, Unlimited, and Developer Editions. On account record page, clicking on Actions dropdown menu will show you the View Account Hierarchy action.
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 |