How to fetch current Record Id and show selected recordId on the page on click button functionality in Lightning Web Component – LWC | How to get selected record Id from custom button click in LWC – Lightning Web Component

8,698 views

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.

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

 

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 →

show selected record id in lwc -- w3web.net

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

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

 

  • 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]

  1.    <template>
  2.     <lightning-card>
  3.     <div class="slds-p-around_large">
  4.         <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>
  5.            <br/>
  6.  
  7.     <table class="tblColPad" border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse;">
  8.         <tr>
  9.             <th>Name</th>
  10.             <th>Industry</th>
  11.             <th>Website</th>
  12.             <th>Type</th>
  13.             <th>Get Record Id</th>
  14.         </tr>
  15.     <template for:each={accounts.data} for:item="acc">
  16.         <tr key={acc.Id}>
  17.             <td>{acc.Name}</td>
  18.             <td>{acc.Industry}</td>
  19.             <td>{acc.Website}</td>
  20.             <td>{acc.Type}</td>
  21.             <td><lightning-button label="Get Record Id" variant="brand" onclick={handleClick} value={acc.Id}></lightning-button></td>
  22.        </tr>
  23.        </template>
  24.        <template if:true={accounts.error}>
  25.         Error in processing the data
  26.        </template>
  27.  
  28.        <tr>
  29.          <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>        
  30.        </tr>
  31.     </table>
  32.  
  33.  
  34.     <br/><br/>
  35.     <!--StartRelatedTopics Section-->
  36. <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
  37.  
  38.         <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>
  39.  
  40.         <br/><br/>
  41.         <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>
  42.         <div style="display:block; overflow:hidden;"> 
  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/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
  46.                     <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>
  47.                     <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>
  48.                     <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>
  49.                     <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>
  50.                 </ul>
  51.         </div>
  52.  
  53.         <div style="width: 50%; float:left; display:inline-block">
  54.                 <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> 
  55.                     <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>
  56.                     <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>
  57.                     <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>
  58.                     <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>
  59.                     <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>
  60.                 </ul>
  61.             </div>
  62.            <div style="clear:both;"></div> 
  63.            <br/>
  64.             <div class="youtubeIcon">
  65.                 <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>
  66.             </div>
  67. </div>
  68.  
  69. </div>
  70.  
  71. <!--EndRelatedTopics Section-->
  72.  
  73. </div>     
  74.  </lightning-card>
  75. </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]

  1.    import { LightningElement,wire,track } from 'lwc';
  2. import getAccountList from '@salesforce/apex/lwcSelectedRecordIdCtrl.getAccountList';
  3.  
  4. export default class LwcSelectedRecordId extends LightningElement {
  5.     @track recordIdVal
  6.     @wire(getAccountList) accounts;
  7.     handleClick(event)
  8.     {
  9.         const selectedId=event.target.value;
  10.        // alert('#### SelectedRecordId is ####'+selectedId);
  11.  
  12.        this.recordIdVal = selectedId;
  13.     }
  14.  
  15.  
  16. }

 

Create LWC Style CSS →

Step 3:- Create Style CSS : lwcSelectedRecordId.css

SFDX:Lightning Web Component >> New >> lwcSelectedRecordId.css

lwcSelectedRecordId.css [Style CSS]

  1.    table.tblColPad th{background:#ddd;}
  2.    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]

  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>

 

Create Apex Class Controller →

Step 5:- Create Apex Class : lwcSelectedRecordIdCtrl.cls

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

lwcSelectedRecordIdCtrl.cls [Apex Class Controller]

  1.    public WITH sharing class lwcSelectedRecordIdCtrl {
  2.     @AuraEnabled(cacheable=TRUE)
  3.     public  static List<Account> getAccountList()
  4.     {
  5.         List<Account> acclist=[SELECT Name, Industry, Website, TYPE, Phone, Description FROM Account LIMIT 10];
  6.         RETURN acclist;
  7.     }
  8. }

 

Create Lightning Application

Step 6:- Create Lightning Application : lwcSelectedRecordIdApp.app

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

lwcSelectedRecordIdApp.app [Component Application File]

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

 
show selected record id in lwc -- w3web.net
 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

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

 
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