show selected record id in lwc --

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

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


Other related post that would you like to learn in LWC


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


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]


Create LWC Style CSS →

Step 3:- Create Style CSS : lwcSelectedRecordId.css

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

lwcSelectedRecordId.css [Style CSS]


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]


Create Apex Class Controller →

Step 5:- Create Apex Class : lwcSelectedRecordIdCtrl.cls

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

lwcSelectedRecordIdCtrl.cls [Apex Class Controller]


Create Lightning Application

Step 6:- Create Lightning Application :

From Developer Console >> File >> New >> Lightning Application [Component Application File]

show selected record id in lwc --

Further post that would you like to learn in LWC


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

  • Your reaction of the article ▾

Hi, This is Vijay behind the admin and founder of 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

Leave a Comment