Hey guys, today in this post we are going to learn about How to Fetch current Record Id and show selected recordId on the page on click button functionality in Lightning Web Component – LWC.
Each record in the Salesforce.com system has a unique ID field assigned to it which is known as Record ID.
It is system generated and cannot be edited or deleted.
It is generated every time a new record is inserted into the application.
Add the force:hasRecordId interface to an Aura component to enable the component to be assigned the ID of the current record. The current record ID is useful if the component is used on a Lightning record page, as an object-specific custom action or action override in Lightning Experience or the Salesforce mobile app, and so on. This interface has no effect except when used within Lightning Experience, the Salesforce mobile app, and Aura-based Experience Builder sites. To know more details about Has Record Id in Lightning Component, Click Here →
Files we used to show selected recordId in LWC →
lwcSelectedRecordId.html | LWC HTML File | Template HTML file to show selected recordId in LWC |
lwcSelectedRecordId.js | LWC JavaScript File | In the javascript file create handleClick function in LWC |
lwcSelectedRecordId.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
lwcSelectedRecordId.css | Style CSS | It is used to creaet custom table border, padding style CSS property. |
lwcSelectedRecordIdApp.app | Lightning Application | It is used to call the component to preview on browser. |
lwcSelectedRecordIdCtrl.cls | Apex Controller | It is used for fetching Account records through @wire decorators from database. |
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 Web Component HTML →
Step 1:- Create Lightning Web Component : lwcSelectedRecordId.html
SFDX:Lightning Web Component >> New >> lwcSelectedRecordId.html
lwcSelectedRecordId.html [Lightning Web Component HTML]
<template>
<lightning-card>
<div class="slds-p-around_large">
<h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom109" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> How to get the selected RecordId on click button in LWC </strong></h3>
<br/>
<table class="tblColPad" border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse;">
<tr>
<th>Name</th>
<th>Industry</th>
<th>Website</th>
<th>Type</th>
<th>Get Record Id</th>
</tr>
<template for:each={accounts.data} for:item="acc">
<tr key={acc.Id}>
<td>{acc.Name}</td>
<td>{acc.Industry}</td>
<td>{acc.Website}</td>
<td>{acc.Type}</td>
<td><lightning-button label="Get Record Id" variant="brand" onclick={handleClick} value={acc.Id}></lightning-button></td>
</tr>
</template>
<template if:true={accounts.error}>
Error in processing the data
</template>
<tr>
<td colspan="5" style="text-align: center; font-size:17px;"><span style="font-weight:bold;">Selected Record Id → </span> <strong style="color:#ff0000;">{recordIdVal}</strong></td>
</tr>
</table>
<br/><br/>
<!--StartRelatedTopics 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/tree-grid-dynamic-expand-collapse-in-lwc/" target="_blank" rel="noopener noreferrer">how to create tree grid with expanded/collapsed section for the entire row in LWC</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>
<!--EndRelatedTopics Section-->
</div>
</lightning-card>
</template>
Create Lightning Web Component JavaScript →
Step 2:- Create Lightning Web Component : lwcSelectedRecordId.js
SFDX:Lightning Web Component >> New >> lwcSelectedRecordId.js
lwcSelectedRecordId.js [LWC JavaScript File]
import { LightningElement,wire,track } from 'lwc';
import getAccountList from '@salesforce/apex/lwcSelectedRecordIdCtrl.getAccountList';
export default class LwcSelectedRecordId extends LightningElement {
@track recordIdVal
@wire(getAccountList) accounts;
handleClick(event)
{
const selectedId=event.target.value;
// alert('#### SelectedRecordId is ####'+selectedId);
this.recordIdVal = selectedId;
}
}
Create LWC Style CSS →
Step 3:- Create Style CSS : lwcSelectedRecordId.css
SFDX:Lightning Web Component >> New >> lwcSelectedRecordId.css
lwcSelectedRecordId.css [Style CSS]
table.tblColPad th{background:#ddd;}
table.tblColPad th, table.tblColPad td {padding:5px !important;}
Create Lightning Web Component Meta XML →
Step 4:- Create Lightning Web Component : lwcSelectedRecordId.js-meta.xml
SFDX:Lightning Web Component >> New >> lwcSelectedRecordId.js-meta.xml
lwcSelectedRecordId.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>
Create Apex Class Controller →
Step 5:- Create Apex Class : lwcSelectedRecordIdCtrl.cls
From Developer Console >> File >> New >> Apex Class
lwcSelectedRecordIdCtrl.cls [Apex Class Controller]
public WITH sharing class lwcSelectedRecordIdCtrl {
@AuraEnabled(cacheable=TRUE)
public static List<Account> getAccountList()
{
List<Account> acclist=[SELECT Name, Industry, Website, TYPE, Phone, Description FROM Account LIMIT 10];
RETURN acclist;
}
}
Create Lightning Application
Step 6:- Create Lightning Application : lwcSelectedRecordIdApp.app
From Developer Console >> File >> New >> Lightning Application
lwcSelectedRecordIdApp.app [Component Application File]
<aura:application extends="force:slds">
<c:lwcSelectedRecordId/>
</aura:application>
Further post that would you like to learn in Salesforce
How do you get record ID and object name in LWC?
To get the current Record Id and Object API Name, we have to declare the respective properties with @api annotation just like @api recordId, @api objectApiName and we need to place the component into Lightning Record Page.
What are the two ways to get the record id?
There are two ways to find them: Go to the Record Type (Setup> Customize> (object)> Record Types). Click on the record type. Find the Record Type ID in the URL between id= and &type.
What is the use of @wire in LWC?
Wiring a property with @wire is useful when you want to consume the data or error. If the property decorated with @wire is used as an attribute in the template and its value changes, the wire service provisions(requests) the data and triggers the component to rerender.
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 |