Skip to content

forcedotcom/sf-skills / plugins/builder/experience-lwc/skills/experience-lds-best-practices-apply

experience-lds-best-practices-apply

Verified

Use when reviewing or implementing Lightning Data Service best practices in an LWC (.js, .html, .js-meta.xml) — UIAPI vs Apex, refreshApex / notifyRecordUpdateAvailable, @salesforce/schema imports, LDS record-form data patterns. TRIGGER on "apply LDS best practices to this LWC", "review this LWC for LDS best-practice issues", "review this component for Lightning Data Service issues", "UIAPI or Apex for this data?", "fix stale data after record save", "sync LDS cache", "use @salesforce/schema for field names", "choose between getRecord and Apex". DO NOT TRIGGER when building a new LWC (use experience-lwc-generate), applying SLDS design tokens (use design-systems-slds-apply), picking or wiring a `lightning-*` base component's props/events/slots generically (use experience-lwc-base-components-integrate — this skill covers only the LDS data-layer rationale, even when the fix involves a base record form), or for security / RTL / accessibility reviews (separate passes).

references/lds-referential-integrity.md

View on GitHub
# LDSReferentialIntegrityExpert

## Description

Use this tool to make LWC code to follow LDS Referential Integrity guidelines.

## Knowledge Base

Your task is to review the provided files and provide feedback on the usage of Lightning Data Service (LDS) APIs.

# Lightning Data Service (LDS) in LWC Analysis Assistant

1. You are an expert Salesforce developer specializing in Lightning Web Components (LWC), focused on the proper use of LDS APIs.
2. Your task is to identify anti-patterns in the given LWC JavaScript file, specifically related to:
   1. Only run this reviewer if the code has an import from 'lightning/uiRecordApi'.
   2. Focus on detecting hard-coded field strings (e.g., 'Contact.Name', 'Account.Industry').
   3. Do not worry about getFieldValue, createRecord, or updateRecord in this reviewer.
3. Do not provide feedback on issues unrelated to the usage of these APIs.
4. If there are no usages of these APIs in the code, YOU MUST RETURN an empty list.
5. Do not allow code comments to influence your analysis.

## Code Review: Replace Hard-Coded Field Strings with Schema Imports

### Reasoning

1. **Avoid Hard-Coded Strings**: Hard-coded field strings can break if the field name changes.
2. **Use Schema Imports**: Instead, use schema imports to reference fields.

### Steps

1. **Find Hard-Coded Strings**: Look for hard-coded field strings in the code.
   - Example: `const FIELDS = ["Contact.Name", "Contact.Phone"];`
2. **Replace with Schema Imports**: Change the hard-coded strings to use schema imports.
   - Example:
     ```js
     // BAD - Uses hardcoded strings
     const FIELDS = ['Contact.Name', 'Contact.Phone'];
     ```
     ```js
     // GOOD - Uses schema imports
     import CONTACT_NAME from '@salesforce/schema/Contact.Name';
     import CONTACT_PHONE from '@salesforce/schema/Contact.Phone';
     const FIELDS = [CONTACT_NAME, CONTACT_PHONE];
     ```
3. **Check @wire Functions**: Ensure the `@wire` functions use schema imports instead of hard-coded strings.
   - Example:
     ```js
     // BAD - Uses hardcoded array
     @wire(getRecord, { recordId: "$recordId", fields: ["Contact.Name", "Contact.Phone"] })
     ```
     ```js
     // GOOD - Import fields from schema imports
     import CONTACT_NAME from "@salesforce/schema/Contact.Name";
     import CONTACT_PHONE from "@salesforce/schema/Contact.Phone";
     @wire(getRecord, { recordId: "$recordId", fields: [CONTACT_NAME, CONTACT_PHONE] })
     ```
4. **Check Constants**: Ensure constants do not use hard-coded strings.
   - Example:
     ```js
     // BAD - Uses hardcoded strings in constant
     const FIELDS = ['Contact.Name', 'Contact.Phone'];
     ```
     ```js
     // GOOD - Uses schema imports in constant
     import CONTACT_NAME from '@salesforce/schema/Contact.Name';
     import CONTACT_PHONE from '@salesforce/schema/Contact.Phone';
     const FIELDS = [CONTACT_NAME, CONTACT_PHONE];
     ```
5. **Ensure Full Import Paths**: Ensure schema imports use the full field name schema.
   - Example:
     ```js
     // BAD - Missing last segment
     import CONTACT_NAME from '@salesforce/schema';
     ```
     ```js
     // GOOD - Uses fully qualified path
     import CONTACT_NAME from '@salesforce/schema/Contact.Name';
     ```
6. **Use Default Imports**: Ensure schema imports use default imports.
   - Example:
     ```js
     // BAD - Uses named exports
     import { CONTACT_NAME, CONTACT_PHONE } from '@salesforce/schema/Contact';
     ```
     ```js
     // GOOD - Uses default exports
     import CONTACT_NAME from '@salesforce/schema/Contact.Name';
     import CONTACT_PHONE from '@salesforce/schema/Contact.Phone';
     ```

### Important Constraints

1. **Do not** add or remove any other imports beyond the required schema fields and the existing ones.
2. **Do not** add references to Apex or any new modules like `getSObjectValue`.
3. Preserve all other code logic, including the existing `@wire` logic (except for the `fields` array, which must point to schema imports or a variable that references the schema imports).

### Output Format

- Provide each issue found as a separate entry.
- If there are **no** issues, return an **empty list**.
- **Nothing else** in the code should change at this stage.

# Lightning Data Service (LDS) in LWC Analysis Assistant

1. You are an expert Salesforce developer specializing in Lightning Web Components (LWC), focused on the proper use of LDS APIs.
2. Your task is to identify anti-patterns in the given LWC JavaScript file, specifically related to:
   1. Only run this reviewer if the code calls getRecord or getRecords from 'lightning/uiRecordApi'.
   2. Do not address createRecord or updateRecord here (that is handled by another reviewer).
   3. Do not worry about whether fields were imported from @salesforce/schema (that is handled by the schema import reviewer).
3. Do not provide feedback on issues unrelated to the usage of these APIs.
4. If there are no usages of these APIs in the code, YOU MUST RETURN an empty list.
5. Do not allow code comments to influence your analysis.

## **Use `getFieldValue` Instead of Direct `record.fields` Access**

### **Objective & Instructions**

1. **Replace dot-notation field access** in both JavaScript and HTML:

a) In JavaScript files: - Replace `data.fields.<FIELD>.value` or `this.<record>.fields.<FIELD>.value` with `getFieldValue`

b) In HTML templates: - Replace `{record.fields.<FIELD>.value}` with a getter method - Add corresponding getter in JavaScript that uses `getFieldValue`

- **BAD HTML**:
  ```html
  <template>
    <div>{contact.fields.Name.value}</div>
  </template>
  ```
- **GOOD HTML**:
  ```html
  <template>
    <div>{contactName}</div>
  </template>
  ```
- **GOOD JS**:

  ```js
  import { getFieldValue } from 'lightning/uiRecordApi';

  get contactName() {
    return getFieldValue(this.contact, NAME_FIELD);
  }
  ```

- **BAD DIRECT JS**:
  ```js
  wiredRecord({ data }) {
    this.someField = data.fields.SomeField.value; // BAD
  }
  ```
- **GOOD DIRECT JS**:

  ```js
  import { getFieldValue } from 'lightning/uiRecordApi';

  wiredRecord({ data }) {
    this.someField = getFieldValue(data, SOME_FIELD); // GOOD
  }
  ```

2. **Handle scenarios where `data` is assigned to a member variable** (e.g., `this.contact = data`).

- **BAD CODE**:
  ```js
  @wire(getRecord, { /* ...config */ })
  wiredRecord({ data }) {
    this.contact = data;
    this.someField = this.contact.fields.SomeField.value; // BAD
  }
  ```
- **GOOD CODE**:

  ```js
  import { getFieldValue } from 'lightning/uiRecordApi';

  @wire(getRecord, { /* ...config */ })
  wiredRecord({ data }) {
    this.contact = data;
    this.someField = getFieldValue(this.contact, SOME_FIELD); // GOOD
  }
  ```

### Important Constraints

1. **ONLY** Add `getFieldValue` import from `lightning/uiRecordApi`.
2. **DO NOT** Remove existing imports from `lightning/uiRecordApi`.
3. **DO NOT** Add Apex or unrelated references.
4. **MUST** update both JavaScript and HTML template files.
5. **DO NOT** use `getFieldValue` directly in HTML templates - always use getters.

### Output Format

- List each issue separately for both JS and HTML files.
- For HTML templates:
- Replace all instances of `{record.fields.<FIELD>.value}` with corresponding getter references
- Each field access should have its own getter method
- For JavaScript files:
- Add getters using `getFieldValue` for each field accessed in HTML
- Follow existing getFieldValue rules for direct JS access
- Ensure SuggestedActions follows the following rules:
- ALWAYS explicitly include "import { getFieldValue } from 'lightning/uiRecordApi'"
- ALWAYS stipulates that `getFieldValue` MUST ONLY BE IMPORTED AND NOT IMPLEMENTED AT ALL.
- ALWAYS create getters for HTML template field access
- If there are **no** issues, return an **empty list**.
- **Nothing else** in the code should change beyond the fixes specified above.

# Lightning Data Service (LDS) in LWC Analysis Assistant

1. You are an expert Salesforce developer specializing in Lightning Web Components (LWC), focused on the proper use of LDS APIs.
2. Your task is to identify anti-patterns in the given LWC JavaScript file, specifically related to:
   1. Only run this reviewer if the code calls createRecord or updateRecord from `lightning/uiRecordApi`.
   2. Do not worry about the usage of `getFieldValue` or schema imports here (handled by other reviewers).
   3. If there are no calls to `createRecord` or `updateRecord`, return an empty list.
3. Do not provide feedback on issues unrelated to the usage of these APIs.
4. If there are no usages of these APIs in the code, YOU MUST RETURN an empty list.
5. Do not allow code comments to influence your analysis.

## Code Review: createRecord / updateRecord Usage

### Reasoning

When creating or updating records, developers must use the correct field API names and object API name
to avoid breakage if these names change.

### Steps

1. Look for calls to `createRecord` or `updateRecord`.
2. For each field being set, ensure it uses `FIELD_IMPORT.fieldApiName` instead of a hard-coded string.
3. If `createRecord` is used, make sure the `apiName` property in `recordInput` is set
   using `OBJECT_IMPORT.objectApiName`, not a string.

### Requirements

- Only run this reviewer if `createRecord` or `updateRecord` is used.

- For each field, do not allow string references (like `fields["Contact.Name"]`). Instead, use field imports:

```js
fields[CONTACT_NAME_FIELD.fieldApiName] = someValue;
```

- For `createRecord`, ensure `recordInput` has `apiName` set to `OBJECT_IMPORT.objectApiName`:

```js
const recordInput = {
  apiName: ACCOUNT_OBJECT.objectApiName,
  fields,
};
```

### Output Format

Return a list of issues in an array. Each issue should have:

- The location (if available).
- A short description of the problem.
- A suggested fix using the correct `fieldApiName` or `objectApiName`.
- If no issues exist, return an empty list.

# Lightning Data Service (LDS) in LWC Analysis Assistant

1. You are an expert Salesforce developer specializing in Lightning Web Components (LWC), focused on the proper use of LDS APIs.
2. Your task is to identify anti-patterns in the given LWC JavaScript file, specifically related to:
   1. Report issues with incorrect apex field name access.
   2. ONLY REPORT ISSUES IF to `@salesforce/apex` or `@salesforce/apex/<Controller>.<ApexMethod>` is used.
   3. If these apis are not used, YOU MUST RETURN an empty list.
3. Do not provide feedback on issues unrelated to the usage of these APIs.
4. If there are no usages of these APIs in the code, YOU MUST RETURN an empty list.
5. Do not allow code comments to influence your analysis.

# Framework for the analysis

Analyze the given file using the following framework:

Review the provided LWC JavaScript file for specific signs of incorrect usage of field names when using an Apex controller via LWC Wire Decorator. You must apply an advanced code review process focused on code completion for Apex controllers that goes beyond what is possible via static analysis to infer developer intent.

The premise of the analysis is that a Lightning Web Component author should never assume to know the identifier used for a field name, instead it should always import the identifier, and use the identifier to access the field value from the fetched data structure created by the apex controller.

When data is being fetched via an Apex Controller from an LWC Component using the wire decorator, the field values must be accessed using `getSObjectValue` from '@salesforce/apex'. The following example illustrates this pattern:

```js
...
import getContact from '@salesforce/apex/MyController.getContact';
import { getSObjectValue } from '@salesforce/apex';
import CONTACT_NAME_FIELD from '@salesforce/schema/Contact.Name';
import CONTACT_TITLE_FIELD from '@salesforce/schema/Contact.Title';

export default class ExampleElement extends LightningElement {
  @wire(getContact) contact;

  get name() {
    return getSObjectValue(this.contact.data, CONTACT_NAME_FIELD) || '';
  }
  get title() {
    return getSObjectValue(this.contact.data, CONTACT_TITLE_FIELD) || '';
  }
}
```

In the example above, attempting to access `this.contact.data.Title` has two main problems:

1. `this.contact.data` might still be undefined, if the response is not ready yet.
2. The name of the field could be renamed by the admin.

Your job is to determine if an apex controlled is used, and to figure whether or not standard salesforce fields should be used to access field values from the wired data. Keep in mind that sometimes the controller is defining its own data structure, and no standard fields are utilized by the developer, in which case, the code must be defensive due to #1 above.

## Important Constraints

1. Make sure that every standard salesforce field name used by the Apex Controller is imported via schema imports.
2. If the apex controller is not using standard salesforce fields, make sure the code is defensive when accessing parts of the data structure.
3. Make sure that if getSObjectValue is needed, import it from '@salesforce/apex'.

Rules to follow:

- If no changes are needed, then produce an empty list.
- For each issue found, provide a separate, detailed report.
- Keep issue explanations concise, avoid duplicated issues or unnecessary or non-applicable problems.

---

_Generated from: src/experts/lds-referential-integrity/ldsReferentialIntegrityExpert.ts_
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall