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
You can download file directly from github by Click Here.
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]
<template>
<lightning-card title="Display the Contacts based on AccountId the help of event in LWC" custom-icon="custom:icon13">
<table class="slds-table slds-table_cell-buffer slds-table_bordered" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse;">
<thead>
<tr>
<th>Select</th>
<th>Name</th>
<th>Phone</th>
<th>Industry</th>
<th>Description</th>
</tr>
</thead>
<template for:each={accData.data} for:item="accItem">
<tr key={accItem.Id}>
<td><lightning-input type="radio" name="radioButtonSelect" value={accItem.Id} onchange={handleChangeRadio}></lightning-input></td>
<td>{accItem.Name}</td>
<td>{accItem.Phone}</td>
<td>{accItem.Industry}</td>
<td>{accItem.Description}</td>
</tr>
</template>
</table>
<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-->
</lightning-card>
</template>
Step 2:- Create Lightning Web Component : displayContactsOnAccountId.js
SFDX:Lightning Web Component >> New >> displayContactsOnAccountId.js
displayContactsOnAccountId.js [LWC JavaScript File]
import { LightningElement, track, wire } from 'lwc';
import retrieveAccountRecords from '@salesforce/apex/lwcAppExampleApex.retrieveAccountRecords';
export default class DisplayContactsOnAccountId extends LightningElement {
@wire (retrieveAccountRecords) accData;
@track getAccId;
handleChangeRadio(event){
this.getAccId = event.target.value;
window.console.log('getAccId ' + this.getAccId);
const myCustomEventItem = new CustomEvent('myeventdemo',{
detail: this.getAccId
});
this.dispatchEvent(myCustomEventItem);
}
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</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]
<template>
<lightning-card>
<h3 slot="title">
<lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> Parent Component
</h3>
<div class="slds-m-bottom--medium">
<c-display-contacts-on-account-id onmyeventdemo={handleChangeAction}></c-display-contacts-on-account-id>
</div>
<br/><br/>
<lightning-icon icon-name="utility:connected_apps" size="small"></lightning-icon> <b>Child Component</b>
<table class="slds-table slds-table_cell-buffer slds-table_bordered" border="1" cellspacing="0" cellpadding="0" bordercolor="#ccc" style="border-collapse:collapse;">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
</tr>
</thead>
<template for:each={records} for:item='conItem'>
<tr key={conItem.Id}>
<td>{conItem.FirstName}</td>
<td>{conItem.LastName}</td>
<td>{conItem.Email}</td>
<td>{conItem.Phone}</td>
</tr>
</template>
</table>
</lightning-card>
</template>
Step 5:- Create Lightning Web Component : parentCmpLwc.js
SFDX:Lightning Web Component >> New >> parentCmpLwc.js
parentCmpLwc.js [LWC JavaScript File]
import { LightningElement, track, wire } from 'lwc';
import retrieveContactRecords from '@salesforce/apex/lwcAppExampleApex.retrieveContactRecords';
export default class ParentCmpLwc extends LightningElement {
@track accountId;
@track records;
@track errorMsg;
@wire (retrieveContactRecords, {accId:'$accountId'})
wireConRecord({error,data}){
if(data){
this.records = data;
this.errorMsg = undefined;
}else{
this.errorMsg = error;
this.records = undefined;
}
}
handleChangeAction(event){
this.accountId = event.detail;
window.console.log('accountId ' + this.accountId);
}
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Start Apex Controller
Step 7:- Create Apex Controller : lwcAppExampleApex.cls
SFDX:Create Apex Class >> New >> lwcAppExampleApex.cls
lwcAppExampleApex.cls [Apex Class]
public WITH sharing class lwcAppExampleApex {
//Display the Contacts based ON AccountId the help OF event IN LWc
@AuraEnabled(cacheable=TRUE)
public static List<Account> retrieveAccountRecords(){
List<Account> accList = [SELECT Id, Name, Phone, Industry, Description FROM Account WHERE Phone != NULL LIMIT 6];
RETURN accList;
}
@AuraEnabled(cacheable=TRUE)
public static List<Contact> retrieveContactRecords(string accId){
List<Contact> conObj = NEW List<Contact>();
List<Contact> conList = [SELECT Id, FirstName, LastName, Email, Phone, AccountId FROM Contact WHERE AccountId=:accId];
FOR(Contact con:conList){
conObj.add(con);
}
RETURN conObj;
}
}
Start Lightning Application
Step 8:- Create Lightning Application : eventAppLwc.app
From Developer Console >> File >> New >> Lightning Application
eventAppLwc.app [Component Application File]
<aura:application extends="force:slds">
<c:parentCmpLwc/>
</aura:application>
Further post that would you like to learn in Salesforce
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
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 |