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[]
  • 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[]

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.


import {
} from 'lwc';
import {
} 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']

    get department(){
    get name() {
     // If you use field option to fetch record you can directly access Name by using
    // since we are specifying this in field array
        var name =  +' ';
        return name;

    get title() {

    get phone() {

    get email() {
hire salesforce developers


    <lightning-card title="Lightning Data Service">
        <template if:true={}> 
            <div class="slds-m-around_medium">
                    <lightning-formatted-phone value={phone}></lightning-formatted-phone>
                    <lightning-formatted-email value={email}></lightning-formatted-email>
                    <lightning-formatted-text value={department}></lightning-formatted-text>

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.

Learn more: How To Create A Custom Report Type In Salesforce and How To Do Component Communication In LWC

Avatar photo

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.