Lightning Data Service (LDS) – Avoid Unnecessary Apex

Lightning Data Service provides built-in record access and caching without Apex. LDS is ideal for simple record display and editing.

1. Basic getRecord Usage


import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const FIELDS = ['Account.Name', 'Account.Phone', 'Account.Industry'];

export default class AccountViewer extends LightningElement {
    @api recordId;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    account;
}
  

Template


<template>
    <template if:true={account.data}>
        <p>Name: {account.data.fields.Name.value}</p>
        <p>Phone: {account.data.fields.Phone.value}</p>
    </template>
    <template if:true={account.error}>
        <p class="error">Error loading record</p>
    </template>
</template>
  

2. Record Edit Form

Use lightning-record-edit-form to handle record updates without Apex.


<template>
    <lightning-record-edit-form 
        record-id={recordId}
        object-api-name="Account"
        onsuccess={handleSuccess}
        onerror={handleError}
    >
        <lightning-messages></lightning-messages>
        <lightning-input-field field-name="Name"></lightning-input-field>
        <lightning-input-field field-name="Phone"></lightning-input-field>
        <lightning-button type="submit" label="Save" variant="brand"></lightning-button>
    </lightning-record-edit-form>
</template>
  

3. getObjectInfo for Metadata


import { LightningElement, wire } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class AccountInfo extends LightningElement {
    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    objectInfo;
}
  

4. getPicklistValues


import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';

export default class IndustrySelector extends LightningElement {
    @track industryOptions;

    @wire(getPicklistValues, {
        recordTypeId: '012000000000000AAA',
        fieldApiName: INDUSTRY_FIELD
    })
    wiredPicklist({ error, data }) {
        if (data) {
            this.industryOptions = data.values;
        }
    }
}
  

5. Advantages of LDS

6. When to Use Apex Instead

Use Apex when data requires complex aggregation, custom search, or non-CRUD business logic.

Key Takeaways