How to fetch current record based on Record Id uses of lightning web component and apex class method in LWC Salesforce | How to pass dynamic record id and display current record based on Record Id in lwc Salesforce | How to fetch and display record data in lightning web component – LWC


Hey guys, today in this post we are going to learn about How to fetch current record based on Record Id uses of lightning web component and apex class method in LWC Salesforce.

RecordId property is used in the lightning record page, We are using this property in a JavaScript class using a @api and @track decorator, and we need to define recordId in public property.

Use this wire adapter to get a recordโ€™s data. To more details about getRecord in LWC, Click Here.


Final Output โ†’

fetch current record id from lwc --


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 : fetchRecordByIdLwc.html

SFDX:Lightning Web Component >> New >> fetchRecordByIdLwc.html

fetchRecordByIdLwc.html [Lightning Web Component HTML]

  1.    <template>
  2.     <lightning-card title="Fetch Record from Record Id in LWC">
  4.         <table border="0" cellspacing="0" cellpadding="0" class="slds-table slds-table_bordered slds-table_col-bordered" style="border-collapse:collapse;">
  5.             <tr>
  6.                 <th><strong>Name</strong></th>
  7.                 <th><strong>Lead Source</strong></th>
  8.                 <th><strong>Stage Name</strong></th>
  9.                 <th><strong>Account Name</strong></th>
  10.                 <th><strong>Description</strong></th>
  11.             </tr>
  12.             <tr for:each={oppItemArr} for:item='recItem' key={rowId} for:index='index' >
  13.                 <td>{recItem.Name}</td>
  14.                 <td>{recItem.LeadSource}</td>
  15.                 <td>{recItem.StageName}</td>
  16.                 <td>{recItem.Account.Name}</td>
  17.                 <td>{recItem.Description}</td>
  18.              </tr>
  19.         </table>   
  20.     </lightning-card>
  21. </template>


Create Lightning Web Component JavaScript

Step 2:- Create Lightning Web Component : fetchRecordByIdLwc.js

SFDX:Lightning Web Component >> New >> fetchRecordByIdLwc.js

fetchRecordByIdLwc.js [LWC JavaScript File]

  1.    import { LightningElement, track,api, wire } from 'lwc';
  2. import getOptRec from '@salesforce/apex/fetchRecordByIdLwcCtrl.getOptRec';
  3. export default class FetchRecordByIdLwc extends LightningElement {
  5.     @api recordId; 
  6.     @track oppItemArr=[];
  8.     @wire(getOptRec,{recId:'$recordId'})
  9.     getInfos({error,data}){
  10.         if(error){
  11.             console.log('error == '+JSON.stringify(error));
  12.         }else if(data){
  13.             console.log('data == ', JSON.stringify(data));
  14.             this.oppItemArr = JSON.parse(JSON.stringify(data));
  15.           return;
  17.         }
  18.     }
  19. }


Create Lightning Web Component Meta XML

Step 3:- Create Lightning Web Component : fetchRecordByIdLwc.js-meta.xml

SFDX:Lightning Web Component >> New >> fetchRecordByIdLwc.js-meta.xml

fetchRecordByIdLwc.js-meta.xml [LWC Meta Data XML]

  1.    <?xml version="1.0" encoding="UTF-8"?>
  2. <LightningComponentBundle xmlns="">
  3.     <apiVersion>56.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>


Step 4:- Create Apex Controller : fetchRecordByIdLwcCtrl.cls

SFDX:Create Apex Class >> New >> fetchRecordByIdLwcCtrl.cls

fetchRecordByIdLwcCtrl.cls [Apex Class]

  1.    public WITH sharing class fetchRecordByIdLwcCtrl {
  3.     @AuraEnabled(cacheable=TRUE)
  4.     public static List<Opportunity> getOptRec(Id recId){
  5.         List<Opportunity> optRecItem = [SELECT Id, Name, LeadSource, StageName, Description, CreatedDate, AccountId, Account.Name  FROM Opportunity WHERE Id=:recId];
  6.         RETURN optRecItem;
  7.     }
  9. }


Further post that would you like to learn in Salesforce



FAQ (Frequently Asked Questions)

How do you get the current record ID in lightning component?

The component's controller can access the ID of the current record from the recordId attribute, using component. get('v. recordId') . The recordId attribute is automatically added to the component by the force:hasRecordId interface.

Can we get record ID in before insert in Salesforce?

You cannot query the records being inserted in a before insert trigger. They are not yet committed to the database. If you are trying to compare at the data before and after the update you likely will be able to find what you want in new and old context variables.

How do I get field name from record ID in Salesforce?

According to this article from Salesforce, you can execute the following snippet of code in the Developer Console in order to find the Object name based on the Record Id prefix: String objectName = SchemaGlobalDescribe.

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


Hi, This is Vijay Kumar 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