Lightning Data Service (LDS) Patterns - MCP Knowledge

Overview

Guidelines for building, reviewing, and maintaining Lightning Web Components that interact with Salesforce’s Lightning Data Service, focusing on data consistency, referential integrity, and choosing UIAPI vs Apex.

Source: Salesforce MCP Service - mcp_salesforce_guide_lds_development, mcp_salesforce_guide_lds_data_consistency, mcp_salesforce_guide_lds_referential_integrity

Core Principles

Data Consistency

Referential Integrity

Choosing UIAPI vs Apex

Pitfall Correct Approach
Overusing Apex instead of LDS Replace simple SOQL Apex calls with UIAPI wire adaptors (getRecord, getObjectInfo, etc.) or base record form components
Stale Data due to Caching After any record mutation call refreshApex(wiredResult) or notifyRecordUpdateAvailable([{recordId}])
Mixing Apex & LDS without Sync Whenever Apex mutates data also refresh the LDS cache, e.g., call refreshApex on wired results or publish notifyRecordUpdateAvailable
Hand-rolling simple forms Use lightning-record-form, lightning-record-edit-form, or lightning-record-view-form which provide built-in validation and SLDS styling
Not importing schema references import NAME_FIELD from '@salesforce/schema/Account.Name' and use constants instead of string literals like 'Name'

Review Checklist

1. Hand-Rolling Forms Instead of Using Base Components

2. Not Importing References to Salesforce Objects and Fields

3. Mixing Apex Calls with LDS Without Synchronization

4. Overusing Apex Instead of LDS

Best Practices

Schema Imports

// Good: Import schema references
import NAME_FIELD from '@salesforce/schema/Account.Name';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

// Bad: Hard-coded strings
const fieldName = 'Name';
const objectName = 'Account';

Data Refresh

// After Apex mutation, refresh LDS cache
import { refreshApex } from '@salesforce/apex';

@wire(getRecord, { recordId: '$recordId' })
wiredRecord;

async handleUpdate() {
    await updateRecord({ fields: { Id: this.recordId, Name: 'New Name' } });
    // Refresh the wired result
    await refreshApex(this.wiredRecord);
}

Base Components

Integration with Existing RAG

Related Patterns:

How This Complements Existing RAG: