In this blog, we will learn about fetching records by using LDS. In the previous blog, we learned about how to use Lightning Data Service now let’s get record details by LDS into it. We will use Lightning/ui*Api Wire Adapters and Functions.
Let’s check step by step
1. Import this in your js controller.
import { LightningElement, wire } from 'lwc'; import { getRecord } from 'lightning/uiRecordApi';
Here @wire is mandatory because we are going to use a wire adapter. Now use get record function to fetch data.
// one way @wire(getRecord, { recordId: string, fields: string|string[], optionalFields?: string|string[] ) propertyOrFunction
- recordId : This will take a unique record id of your salesforce record
- fields : This will take the array of fields that you want to retrieve. Since we know that LDS follows FLS if you asked for any field that the user has no permission to a field, an error is returned.
- optionalFields : Use this if you do not sure about user permission because if you enter fields in this and user has no permission it will not fetch that field in response but won’t throw any error.
There is another way in which we can call this getRecord function and that is by providing layout type while in the above option we have provided fields. The syntax will be :
// another way @wire(getRecord, { recordId: string, layoutTypes: string|string[], modes?: string|string[], optionalFields?: string|string[] )propertyOrFunction
Also Read: How To Use Database.Stateful In Batch Apex
Points To Note
There are two required arguments for this getRecord call.
1. RecordId
2. Fields/Layout Type (must provide any one of these)
Example: Let’s create a fetchRecord web component to see what we learned till now.
fetchRecord.js
import { LightningElement, api,track, wire } from 'lwc'; import { getRecord } from 'lightning/uiRecordApi'; // const fields = [ // 'Contact.Name', // 'Contact.Title', // 'Contact.Phone', // 'Contact.Email', // 'Contact.Department', // ]; export default class FetchRecord extends LightningElement { @api recordId; @track contactRec; // by providing field in argument. Uncomment this and comment layout type code to check functionality // @wire(getRecord, { // recordId: '$recordId', // fields // }) //By providing layout type in argument @wire(getRecord, { recordId: '$recordId', layoutTypes: ['Full'] }) contactRec; get department(){ return this.contactRec.data.fields.Department.value; } get name() { console.log(this.contactRec.data.fields); // If you use field option to fetch record you can directly access Name by using // this.contactRec.data.fields.Name.value since we are specifying this in field array var name = this.contactRec.data.fields.FirstName.value +' ' +this.contactRec.data.fields.LastName.value; return name; } get title() { return this.contactRec.data.fields.Title.value; } get phone() { return this.contactRec.data.fields.Phone.value; } get email() { return this.contactRec.data.fields.Email.value; } }
fetchRecord.html
<template> <lightning-card title="Lightning Data Service"> <template if:true={contactRec.data}> <div class="slds-m-around_medium"> <p>{name}</p> <p>{title}</p> <p> <lightning-formatted-phone value={phone}></lightning-formatted-phone> </p> <p> <lightning-formatted-email value={email}></lightning-formatted-email> </p> <p> <lightning-formatted-text value={department}></lightning-formatted-text> </p> </div> </template> </lightning-card> </template>
Read the comment section in js file carefully and try by providing field option too for better understanding.
I hope this will help. Next, we will learn about Record Creation using LDS.
We at Emizentech have expertise in providing salesforce development services for almost all of the salesforce products. If you ever need assistance get in touch with our salesforce consultants and developers.