| | |

Salesforce Quick Tip: How to Use Lightning Data Service (LDS) in an LWC to Retrieve Data Without Apex


One of the most powerful features of Lightning Web Components (LWC) is the ability to fetch record data directly from the client side without writing a single line of server-side Apex code.

In this Salesforce Quick Tip, we’ll find out how to use the uiRecordApi with the wire adapter to retrieve and display specific field data—like an Account Name—on a Lightning Record Page.

The Goal

We want to build a simple LWC that sits on a record page (e.g., an Account page) and displays a specific field value (e.g., the Account Name) by dynamically retrieving it using the current record’s ID.

Step 1: The Imports

First, you need to import the necessary modules in your JavaScript file. This includes the wire and api decorators, the specific methods from the uiRecordApi, and references to the fields you want to query.

JavaScript

import { LightningElement, wire, api } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
  • getRecord: The wire adapter used to fetch record data.
  • getFieldValue: A helper function to easily extract the value from the returned record data.
  • NAME_FIELD: Importing the schema reference ensures referential integrity (Salesforce will know this field is being used).

Step 2: Define Fields and Record ID

Next, define the fields you want to fetch and prepare to receive the recordId from the page.

JavaScript

const fields = [NAME_FIELD];

export default class MyComponent extends LightningElement {
    @api recordId; // Automatically populated by the Lightning Record Page

Step 3: Wire the getRecord Service

Now, use the @wire service to call getRecord. You pass it the recordId (reactive, using the $) and the list of fields you defined.

JavaScript

    @wire(getRecord, { recordId: '$recordId', fields })
    account; // The data is stored in this property

Step 4: Create a Getter for the Value

To make the data easy to use in your HTML template, create a getter method. This uses getFieldValue to cleanly extract the specific field data from the wired account property.

JavaScript

    get accountName() {
        return getFieldValue(this.account.data, NAME_FIELD);
    }
}

Step 5: Display in HTML

Finally, in your HTML file, you can simply reference the getter.

HTML

<template>
    <lightning-card title="Account Info">
        <div class="slds-p-around_medium">
            <h1>Account Name: {accountName}</h1>
        </div>
    </lightning-card>
</template>

And that’s it! Now go enjoy retrieving your Salesforce data without the need for utilizing Apex!


For more LWC tutorials and Salesforce development tips, check out the Coding With The Force YouTube channel.


Need help with your Salesforce Org?

If you need help with your Salesforce org, schedule an hour of consulting time with me! I’m one of only ~500 Salesforce Certified Technical Architect’s (CTA) worldwide and I’ve spent over 30,000 hours building Salesforce implementations over the last decade!

Schedule and hour of consulting with me here!


Do you want to be the next Salesforce Certified Technical Architect (CTA)?

If you need training to help you on your journey to complete your Salesforce CTA Board then why not sign up for the cheapest Salesforce CTA course out there, with someone who has training over a million Salesforce professionals worldwide! You can check out and enroll in the course below!

Sign up for the CTA course here!

Or if a course isn’t your thing, you can always sign up for an hour long CTA study session with me here:
Schedule 1-on-1 CTA Coaching Here!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture


AI DISCLAIMER: Gemini assisted me in writing this blog post by analyzing and summarizing the contents on the YouTube video I created that is linked at the top of this post.

Similar Posts