Get a Quote

Hey, do you want to share your amazing ideas with us, drop your requirements, we are keen to work with you on your fantastic project. We will try to reach you shortly, SEE YOU !

Tell us about you

Tell us about your Project

How To Fetch Records By Lightning Data Service In Salesforce

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;
    }
}
hire salesforce developers

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.

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

author profile
About Author
Author Name:
Virendra Sharma

Certified Magento 2 Consultant

Author Bio:

Virendra Sharma is an ecommerce tech-enthusiast and certified Magento Developer. With a decade of experience in Magento ecommerce development, he has been assisting ecommerce businesses across the globe to harness Magento capabilities by developing, maintaining, and improving clients’ eCommerce websites. A leader in his own rights his teammates see him as an avid researcher and a Magento evangelist.

About Emizentech:

An acclaimed IT solution provider and a leading Magento 2 development company with its expertise lies in delivering top of the line ecommerce solutions, Salesforce commerce cloud services, mobile app development and many more verticals.

 
Develop Car Buy Sell App
06 Jul, 2020

Car Buy-Sell Mobile App Development - Cost & Features

Technology has changed the shopping habits and the needs of the customers. It has changed the way people have started […]
Read more
Mobile App Development
Convert Ecommerce Store to Mobile Commerce App
03 Jul, 2020

Why Convert Your Ecommerce Store To Mobile Commerce App

While running towards success, an eCommerce business everyone just dreams about living in the heart of the audience and ruling […]
Read more
Ecommerce Development
On Demand Laundry Mobile App Development Cost & Features
02 Jul, 2020

On-Demand Laundry Mobile App Development - Cost and Features

Home services, handyman services, food, and cab services are amongst the greatest beneficiaries of the evolving on-demand economy. Whether it […]
Read more
Mobile App Development

Let’s Talk Business

Reach out to us by following below given details, or simply drop us a text to start a conversation, we are here to provide best in class business solutions.

SAY SOMETHING