How to Fetch current Record Id from Lightning Web Components (LWC)

By | February 9, 2022

This blog discusses on how to fetch the current record Id in the lightning web component. RecordId property is used in the lightning record page, and the property, i.e., recordId, is set to a current record Id.

We are using this property in a JavaScript class using a @api decorator, and we need to define recordId in public property.

Note: The lightning Web component should be added to a lightning record page.

To get Id from the lightning web component, we need to define a recordId property with @api decorator. Now let us see an example –

  • First, create a lightning web component page on any record page (Account, Opportunity, contact) using Visual studio code.
  • We are creating this LWC to get account details.
  • Give any name to the LWC page as we are giving as recordIdexample. Use Control Shift P and select SFDX: Create lightning Web Component.

Note: Alternative of Using Control Shift P is go to View and select command Palette.

  • We use this command palette to create SFDX: Create lightning Web Component. and authorize our org.
  • Now we are showing an account detail using recordId in lightning record form. We can also use some other details of the Account.

Here is an example of a JavaScript class with the name myFirstLWC.js

LWC Class
LWC Class
  • We are using this <lightning-record-view-form> to show the details of the Account using recordId.
  • We can also use some other details also related to the Account.

Note: Because we are doing for account detail page, you can also do this for contact, opportunity, etc.

Here is an example of an Html file with the name myFirstLWC.html

HTML File
HTML File

myFirstLWC.js-meta.xml

  • For the lightning-record page, we have given target as lightning-recordpage.
  • You can also give these details on the home page or any other page you want.
Lightning Record Page
Lightning Record Page

Now let’s add this LWC on an Account details Page.

  • Go to the Account Page of the Sales.
  • Open any record or Account you want.
  • Then Click on Setup (Gear Icon) and select the edit page option.
  • On the left side, under Custom components, select your myFirstLWC component.
  • Drag and Drop on the right side of the top, click save and activate.
  • Below, you can see the Account detail on a lightning record page of the Account.
Fetch current record id from Lightning Web Components on Account
Fetch current record id from Lightning Web Components on Account

The above steps shows how user can fetch the current record Id from Salesforce using LWC.

We hope that you find this blog helpful, if you still have queries, don’t hesitate to contact us at salesforce@greytrix.com.

About Us
Greytrix – a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce Sage integration for Sage X3Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data MigrationIntegrated App developmentCustom App development and Technical Support to business partners and end users.
Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star rated app listed on Salesforce AppExchange.

The GUMU™ Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.

For more information on our Salesforce products and services, contact us at salesforce@greytrix.com. We will be glad to assist you.

Related Posts