How to display account related contacts based on AccountId using the CustomEvent & dispatchEven in Salesforce lightning web component – LWC | how to display account related contacts based on AccountId in lwc

12,083 views


Hey guys, today in this post we are going to learn about How to display account related contacts based on AccountId using the CustomEvent & dispatchEven in salesforce lightning web component – LWC.

Files we used in this post example:-

displayContactsOnAccountId.html Lightning Web Component HTML It is child HTML file and calling on parentCmpLwc.html.
displayContactsOnAccountId.js Lightning Web Component JavaScript It is holding @wire  property with handleChangeRadio functionality with CustomEvent and dispatchEvent events.
displayContactsOnAccountId.js-meta.xml XML Meta File It is used for where this lightning web component you want to exposed.
parentCmpLwc.html Lightning Web Component HTML It is Parent HTML file, that is used for write HTML element for user interface..
parentCmpLwc.js Lightning Web Component JavaScript It is holding child component’s event value..
parentCmpLwc.js-meta.xml XML Meta File It is used for where this lightning web component you want to exposed.
eventAppLwc.app Lightning Application File It is used for call the parent component to preview on browser..
lwcAppExampleApex.cls Apex Controller It is used for call apex @wire method to retrieve the data from server in LWC Javascript file..

Live Demo

display account related contacts based on AccountId in LWC -- w3web.net

 

You can download file directly from github by Click Here.

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

 
 

Other related post that would you like to learn in Salesforce

Start LWC Child Component

Step 1:- Create Lightning Web Component : displayContactsOnAccountId.html

SFDX:Lightning Web Component >> New >> displayContactsOnAccountId.html

displayContactsOnAccountId.html [Lightning Web Component HTML]

  1.    <template>
  2.    <lightning-card title="Display the Contacts based on AccountId the help of event in LWC" custom-icon="custom:icon13">
  3.       <table class="slds-table slds-table_cell-buffer slds-table_bordered" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse;">
  4.            <thead>
  5.                <tr>
  6.                    <th>Select</th>
  7.                    <th>Name</th>
  8.                    <th>Phone</th>
  9.                    <th>Industry</th>
  10.                    <th>Description</th>                   
  11.                </tr>
  12.            </thead>
  13.             <template for:each={accData.data} for:item="accItem">
  14.                 <tr key={accItem.Id}>
  15.                     <td><lightning-input type="radio" name="radioButtonSelect" value={accItem.Id} onchange={handleChangeRadio}></lightning-input></td>
  16.                     <td>{accItem.Name}</td>
  17.                     <td>{accItem.Phone}</td>
  18.                     <td>{accItem.Industry}</td>
  19.                     <td>{accItem.Description}</td>                    
  20.                 </tr>
  21.             </template>
  22.       </table>
  23.  
  24. <br/><br/>
  25.    <!--Start RelatedTopics Section-->
  26. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  27.  
  28.             <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>
  29.  
  30.             <br/><br/>
  31.             <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>
  32.             <div style="display:block; overflow:hidden;"> 
  33.                 <div style="width: 50%; float:left; display:inline-block">
  34.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  35.                         <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>
  36.                         <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>
  37.                         <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>
  38.                         <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>
  39.                         <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>
  40.                     </ul>
  41.             </div>
  42.  
  43.             <div style="width: 50%; float:left; display:inline-block">
  44.                     <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  45.                         <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>
  46.                         <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>
  47.                         <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>
  48.                         <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>
  49.                         <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>
  50.                     </ul>
  51.                 </div>
  52.                <div style="clear:both;"></div> 
  53.                <br/>
  54.                 <div class="youtubeIcon">
  55.                     <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>
  56.                 </div>
  57.     </div>
  58.  
  59. </div>
  60.  
  61.   <!--End RelatedTopics Section-->
  62.  
  63.    </lightning-card> 
  64. </template>

Step 2:- Create Lightning Web Component : displayContactsOnAccountId.js

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

SFDX:Lightning Web Component >> New >> displayContactsOnAccountId.js

displayContactsOnAccountId.js [LWC JavaScript File]

  1.   import { LightningElement, track, wire } from 'lwc';
  2. import retrieveAccountRecords from '@salesforce/apex/lwcAppExampleApex.retrieveAccountRecords';
  3.  
  4. export default class DisplayContactsOnAccountId extends LightningElement {
  5.     @wire (retrieveAccountRecords) accData;
  6.     @track getAccId;
  7.  
  8.     handleChangeRadio(event){        
  9.         this.getAccId = event.target.value;
  10.         window.console.log('getAccId ' + this.getAccId);
  11.        const myCustomEventItem = new CustomEvent('myeventdemo',{
  12.             detail: this.getAccId
  13.        });
  14.        this.dispatchEvent(myCustomEventItem);
  15.  
  16.     }
  17. }

Step 3:- Create Lightning Web Component : displayContactsOnAccountId.js-meta.xml

SFDX:Lightning Web Component >> New >> displayContactsOnAccountId.js-meta.xml

displayContactsOnAccountId.js-meta.xml [LWC Meta Data XML]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Start LWC Parent Component

Step 4:- Create Lightning Web Component : parentCmpLwc.html

SFDX:Lightning Web Component >> New >> parentCmpLwc.html

parentCmpLwc.html [Lightning Web Component HTML]

  1.   <template>
  2.     <lightning-card>
  3.  
  4.         <h3 slot="title">
  5.             <lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> Parent Component         
  6.         </h3>
  7.  
  8.         <div class="slds-m-bottom--medium">
  9.             <c-display-contacts-on-account-id onmyeventdemo={handleChangeAction}></c-display-contacts-on-account-id>
  10.         </div>
  11.  
  12.         <br/><br/>
  13.  
  14.             <lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> <b>Child Component</b>         
  15.  
  16.  
  17.         <table class="slds-table slds-table_cell-buffer slds-table_bordered" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse;">
  18.            <thead>
  19.                <tr>
  20.                    <th>First Name</th>
  21.                    <th>Last Name</th>
  22.                    <th>Email</th>
  23.                    <th>Phone</th>
  24.                </tr>
  25.            </thead> 
  26.             <template for:each={records} for:item='conItem'>
  27.                 <tr key={conItem.Id}>
  28.                     <td>{conItem.FirstName}</td>
  29.                     <td>{conItem.LastName}</td>
  30.                     <td>{conItem.Email}</td>
  31.                     <td>{conItem.Phone}</td>
  32.                 </tr>
  33.             </template>
  34.         </table>
  35.  
  36.  
  37.     </lightning-card>
  38.  
  39. </template>

Step 5:- Create Lightning Web Component : parentCmpLwc.js

SFDX:Lightning Web Component >> New >> parentCmpLwc.js

parentCmpLwc.js [LWC JavaScript File]

  1.   import { LightningElement, track, wire } from 'lwc';
  2. import retrieveContactRecords from '@salesforce/apex/lwcAppExampleApex.retrieveContactRecords';
  3.  
  4. export default class ParentCmpLwc extends LightningElement {
  5.  
  6.     @track accountId;
  7.     @track records;
  8.     @track errorMsg;    
  9.  
  10.     @wire (retrieveContactRecords, {accId:'$accountId'})
  11.       wireConRecord({error,data}){
  12.         if(data){
  13.           this.records = data;     
  14.           this.errorMsg = undefined;    
  15.         }else{         
  16.           this.errorMsg = error;
  17.           this.records = undefined;
  18.         }
  19.       }
  20.  
  21.     handleChangeAction(event){
  22.       this.accountId = event.detail;
  23.       window.console.log('accountId ' + this.accountId);
  24.     }
  25.  
  26.  
  27. }

Step 6:- Create Lightning Web Component : parentCmpLwc.js-meta.xml

SFDX:Lightning Web Component >> New >> parentCmpLwc.js-meta.xml

parentCmpLwc.js-meta.xml [LWC Meta Data XML]

  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  3.     <apiVersion>45.0</apiVersion>
  4.     <isExposed>true</isExposed>
  5.     <targets>
  6.         <target>lightning__AppPage</target>
  7.         <target>lightning__RecordPage</target>
  8.         <target>lightning__HomePage</target>
  9.       </targets>
  10. </LightningComponentBundle>

Start Apex Controller

Step 7:- Create Apex Controller : lwcAppExampleApex.cls

SFDX:Create Apex Class >> New >> lwcAppExampleApex.cls

lwcAppExampleApex.cls [Apex Class]

  1.   public WITH sharing class lwcAppExampleApex {
  2.  
  3.  
  4.     //Display the Contacts based ON AccountId the help OF event IN LWc
  5.     @AuraEnabled(cacheable=TRUE)
  6.     public static List<Account> retrieveAccountRecords(){
  7.         List<Account> accList = [SELECT Id, Name, Phone, Industry, Description FROM Account WHERE Phone != NULL LIMIT 6];
  8.         RETURN accList;
  9.     }
  10.  
  11.  
  12.     @AuraEnabled(cacheable=TRUE)
  13.     public static List<Contact> retrieveContactRecords(string accId){
  14.       List<Contact> conObj = NEW  List<Contact>();
  15.       List<Contact> conList = [SELECT Id, FirstName, LastName, Email, Phone, AccountId FROM Contact WHERE AccountId=:accId];
  16.       FOR(Contact con:conList){
  17.         conObj.add(con);
  18.       }
  19.       RETURN conObj;
  20.     }
  21. }

Start Lightning Application

Step 8:- Create Lightning Application : eventAppLwc.app

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

eventAppLwc.app [Component Application File]

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

display account related contacts in lightning web component -- w3web.net

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What two types of account contact relationship can you see in contacts to multiple accounts?

Direct Relationship and indirect relationship are the two types of account-contact relationships.

Can a contact be associated with multiple accounts?

When you use Contacts to Multiple Accounts, each contact still requires a primary account (the account in the Account Name field). The contact and its primary account have a direct relationship. But you can add other accounts to the contact. These secondary account-contact relationships are indirect.

How do I get user information in LWC?

To get the current user information, use the @salesforce/user scoped module in LWC.

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