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 β
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]
<template>
<lightning-card title="Fetch Record from Record Id in LWC">
<table border="0" cellspacing="0" cellpadding="0" class="slds-table slds-table_bordered slds-table_col-bordered" style="border-collapse:collapse;">
<tr>
<th><strong>Name</strong></th>
<th><strong>Lead Source</strong></th>
<th><strong>Stage Name</strong></th>
<th><strong>Account Name</strong></th>
<th><strong>Description</strong></th>
</tr>
<tr for:each={oppItemArr} for:item='recItem' key={rowId} for:index='index' >
<td>{recItem.Name}</td>
<td>{recItem.LeadSource}</td>
<td>{recItem.StageName}</td>
<td>{recItem.Account.Name}</td>
<td>{recItem.Description}</td>
</tr>
</table>
</lightning-card>
</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]
import { LightningElement, track,api, wire } from 'lwc';
import getOptRec from '@salesforce/apex/fetchRecordByIdLwcCtrl.getOptRec';
export default class FetchRecordByIdLwc extends LightningElement {
@api recordId;
@track oppItemArr=[];
@wire(getOptRec,{recId:'$recordId'})
getInfos({error,data}){
if(error){
console.log('error == '+JSON.stringify(error));
}else if(data){
console.log('data == ', JSON.stringify(data));
this.oppItemArr = JSON.parse(JSON.stringify(data));
return;
}
}
}
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]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Step 4:- Create Apex Controller : fetchRecordByIdLwcCtrl.cls
SFDX:Create Apex Class >> New >> fetchRecordByIdLwcCtrl.cls
fetchRecordByIdLwcCtrl.cls [Apex Class]
public WITH sharing class fetchRecordByIdLwcCtrl {
@AuraEnabled(cacheable=TRUE)
public static List<Opportunity> getOptRec(Id recId){
List<Opportunity> optRecItem = [SELECT Id, Name, LeadSource, StageName, Description, CreatedDate, AccountId, Account.Name FROM Opportunity WHERE Id=:recId];
RETURN optRecItem;
}
}
Further post that would you like to learn in Salesforce
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 |