How to Get Record ID in LWC in Salesforce (Detailed Guide)
Getting the record ID in Lightning Web Components is essential for building dynamic, context-aware Salesforce apps. This guide explains how to use the @api decorator to access the current record ID both when the component is placed on a record page and when used as a quick action. It provides clear code examples and metadata configurations for embedding LWCs that display record IDs on record pages and quick action panels. After implementing these techniques, Salesforce professionals can create components that interact with the current record context to fetch data or trigger actions smoothly.
- Use @api decorator in LWC to access the current recordId property.
- Expose the LWC on a record page by configuring lightning__RecordPage in meta.xml.
- Create quick action LWCs by setting target to lightning__RecordAction in meta.xml.
- Deploy LWCs to object record pages or quick action layouts to display record IDs.
- Use Lightning App Builder to add LWCs to record page templates and layouts.
When we build Lightning Web Components , getting the recordId is very important. We need it to fetch data, call Apex, show forms, or know which record the user is viewing. For example, imagine you add a Lightning Web Component to the Account record page . When a user opens any Account, the LWC should automatically show all Opportunities related to that Account . To show the correct data, the component must know which Account is currently open. To do this, the component must first get the Record ID . In Salesforce, a record ID is a unique identifier for a Salesforce object . To get or fetch the record ID in a Lightning Web Component, we need to add the component to the Lightning record page of that specific object. In this article, we will learn different ways to get record ID in LWC in Salesforce using practical examples.