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 | How to Fetch/Search all the records in related list on Account on click button in Lightning Component Salesforce

2,560 views


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..

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

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 β†’

how to query account hierarchy 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

 

  • Find the below steps β–Ύ

 

Create Lightning Component

Step 1:- Create Lightning Component : AccountCmp.cmp

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

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

AccountCmp.cmp [Lightning Component File]

  1.   <aura:component controller="accCtrl" 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="accItemList" type="List"/>
  4.     <aura:attribute name="srchList" type="List"/>
  5.  
  6.     <aura:attribute name="conItemList" type="List"/>
  7.  
  8.     <aura:attribute name="modalFade" type="boolean" default="false"/>
  9.  
  10.  
  11.  
  12.     <div class="slds slds-p-around_medium">
  13.  
  14.         <div class="slds-p-bottom_medium"><ui:inputText class="slds-input" aura:id="srhId" keyup="{!c.searchName}" updateOn="keyup" placeholder="Search Name.."/>   </div>
  15.         <table class="slds-table slds-table--bordered slds-table--col-bordered" style="border-collapse:collapse; border:1px #ddd solid;">
  16.             <thead>
  17.                 <tr style="background:#ddd;">
  18.                     <th style="background:#eee;">Name</th>
  19.                     <th style="background:#eee;">Phone</th>
  20.                     <th style="background:#eee;">Industry</th>
  21.                     <th style="background:#eee;"></th>
  22.                 </tr>
  23.             </thead>
  24.             <tbody>
  25.                 <aura:iteration items="{!v.accItemList}" var="obj" indexVar="index">
  26.                     <tr>
  27.                         <td>{!obj.Name}</td>
  28.                         <td>{!obj.Phone}</td>
  29.                         <td>{!obj.Industry}</td>
  30.                         <td><button class="slds-button slds-button_brand" id="{!obj.Id}" onclick="{!c.contactAction}" >Related Contacts</button></td>
  31.                     </tr>                    
  32.                 </aura:iteration>  
  33.             </tbody>
  34.         </table>
  35.  
  36.         &nbsp;&nbsp;
  37.  
  38.  
  39.  
  40.         <aura:if isTrue="{!v.modalFade}"> 
  41.             <section id="modalFade" class="slds-modal slds-fade-in-open" >
  42.                 <div class="slds-modal__container">
  43.                     <header class="slds-modal__header">
  44.                         <button class="slds-button slds-modal__close slds-button--icon-inverse" name="dataRowIcon0" title="Close" onclick="{!c.closePopup}">
  45.                             <lightning:icon iconName="utility:close" variant="bare"  icon-class="slds-m-around_medium" size="small" alternativeText="close"/>
  46.                         </button>
  47.  
  48.                         <h2 class="slds-text-heading_medium slds-hyphenate">Related Contacts</h2>
  49.                     </header>
  50.                     <div class="slds-modal__content slds-p-around_medium">         
  51.                         <table class="slds-table slds-table--bordered slds-table--col-bordered" style="border-collapse:collapse; border:1px #ddd solid;">
  52.  
  53.                             <thead>
  54.                                 <tr style="background:#ddd;">
  55.                                     <th style="background:#eee;">First Name</th>
  56.                                     <th style="background:#eee;">Last Name</th>
  57.                                     <th style="background:#eee;">Phone</th>                  
  58.                                 </tr>
  59.                             </thead>
  60.                             <tbody> 
  61.                                 <aura:iteration items="{!v.conItemList}" var="acItem" indexVar="index">
  62.                                     <aura:iteration items="{!acItem.Contacts}" var="conItem">
  63.                                         <tr>
  64.                                             <td>{!conItem.FirstName}</td>
  65.                                             <td>{!conItem.LastName}</td> 
  66.                                             <td>{!conItem.Phone}</td>
  67.                                         </tr>           
  68.                                     </aura:iteration>    
  69.                                 </aura:iteration>
  70.                             </tbody>
  71.  
  72.                         </table>
  73.  
  74.                     </div>
  75.                     <footer class="slds-modal__footer">
  76.                         <button class="slds-button slds-button--destructive" name="dataRowIcon0" onclick="{!c.closePopup}">Cancel</button>
  77.  
  78.                     </footer>
  79.                 </div>
  80.             </section>
  81.             <div id="modalBackdrop" class="slds-backdrop slds-backdrop_open"></div>
  82.  
  83.         </aura:if>   
  84.  
  85.         &nbsp;&nbsp;
  86.  
  87.     </div>    
  88. </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]

  1.    ({
  2. 	doInit : function(component,event,helper) {
  3. 		 var action = component.get("c.accListData");  
  4.  
  5.         action.setCallback(this,function(response){  
  6.             var state = response.getState();     
  7.             //alert('state' + state);
  8.             if(state=='SUCCESS'){  
  9.                 var result = response.getReturnValue();
  10.                 component.set("v.accItemList",result);  
  11.  
  12.             }  
  13.         });  
  14.         $A.enqueueAction(action);
  15. 	},
  16.  
  17.     searchName:function(component,event,helper){
  18.         var srhId = component.find('srhId').get('v.value');
  19.         //alert('srhId ' + srhId);
  20.         var action = component.get("c.srchAccName");  
  21.         action.setParams({"searchKey":srhId});
  22.         action.setCallback(this,function(response){  
  23.             var state = response.getState();     
  24.            //alert('state' + state);
  25.             if(state=='SUCCESS'){  
  26.                 var result = response.getReturnValue();
  27.                 component.set("v.accItemList",result);                  
  28.             }  
  29.         });  
  30.         $A.enqueueAction(action);
  31.     },
  32.  
  33.  
  34.     contactAction:function(component,event,helper){
  35.         var thisObj = event.target.id;
  36.  
  37.         //alert('srhId ' + srhId);
  38.         var action = component.get("c.AccountNested");  
  39.         action.setParams({"recId":thisObj});
  40.         action.setCallback(this,function(response){  
  41.             var state = response.getState();     
  42.            //alert('state' + state);
  43.             if(state=='SUCCESS'){  
  44.                 var result = response.getReturnValue();
  45.                  //alert(JSON.stringify(result));
  46.                 component.set("v.conItemList",result); 
  47.                 component.set("v.modalFade",true);
  48.  
  49.  
  50.             }  
  51.         });  
  52.         $A.enqueueAction(action);
  53.     },
  54.  
  55.  
  56.  
  57.     closePopup:function(component,event,helper){
  58.         component.set("v.modalFade",false);
  59.     } 
  60.  
  61. })

Create Apex Class Controller

Step 3:- Create Apex Class : accCtrl.apxc

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

accCtrl.apxc [Apex Class Controller]

  1.   public class accCtrl {
  2.  
  3.     @AuraEnabled
  4.     public static List<Account> AccountNested(String recId){
  5.         List<Account> accNestObj = [SELECT Id, Name, Phone, Industry, (SELECT Id, FirstName, LastName, Phone, Email FROM Contacts) FROM Account WHERE Id=:recId];
  6.         RETURN accNestObj;
  7.     }
  8.  
  9.  
  10.     @AuraEnabled
  11.       public static List<Account> accListData() {
  12.         List<Account> accList = [SELECT Id, Name, Phone, Industry FROM Account  WHERE Industry !=NULL];
  13.           RETURN accList;
  14.       }
  15.  
  16.  
  17.     @AuraEnabled
  18.     public static List<Account> srchAccName(String searchKey){
  19. 	searchKey = '%' + searchKey + '%';
  20. 	List<Account> accList = [SELECT Id, Name, Phone, Industry FROM Account WHERE Name LIKE:searchKey];
  21. 	RETURN accList;
  22.  }
  23.  
  24.  
  25. }

Create Lightning Application

Step 4:- Create Lightning Application : AccountCmpApp.app

From Developer Console >> File >> New >> Lightning Application

AccountCmpApp.app [Component Application File]

  1.    <aura:application extends="force:slds" >
  2.      <c:accountCmp/>
  3.   </aura:application>

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

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



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