Sub topic 12
Choosing between @wire, imperative Apex, and Lightning Data Service
This chapter assumes you can write an @AuraEnabled(cacheable=true) Apex method, import it into a component, and wire it to a property or function. Here you will decide which of three data access options fits a given job.
The three options
Lightning Web Components can read and change Salesforce data in three ways.
- Lightning Data Service (LDS) covers the base
lightning-record-form,lightning-record-view-form, andlightning-record-edit-formcomponents, plus wire adapters such asgetRecordfromlightning/uiRecordApi. You write no Apex. LDS enforces sharing and field-level security for you, shares a client cache across components, and keeps records consistent when one component changes them. - Wired Apex uses
@wirewith a cacheable Apex method. The platform calls the method, provisions the result, and calls it again when a reactive parameter changes. - Imperative Apex calls the imported method as a function that returns a promise. You decide when it runs.
Start with LDS
If your need is to show, create, edit, or delete a single record, or to read a few fields from it, use LDS. It needs the least code, and the cache is shared, so a change made in one component shows up in the others.
Move to Apex when LDS cannot express the request. Common reasons are a query with filters or ordering over many records, a join across objects, aggregate results, or logic that must run on the server.
Wire or imperative
Once you need Apex, the question is who decides when the call runs.
Use @wire when the data depends on component state and should stay current with it. The $recordId example from earlier chapters fits: when recordId changes, the call re-runs without any code from you.
Use imperative calls when any of these apply:
- The Apex method changes data. Methods that insert, update, or delete cannot be
cacheable=true, and wire adapters require cacheable methods. - A user action starts the call, such as a button click or a search submit.
- You need to sequence calls, for example calling a second method with the result of the first.
- You want the data fetched once at a chosen time, not whenever the parameters change.
A wire may also run later than you expect. It provisions data after the component connects and the parameters resolve, so a wire is a poor fit for logic that must run at a precise moment.
Comparing them
| Concern | LDS | Wired Apex | Imperative Apex |
|---|---|---|---|
| Apex needed | No | Yes, cacheable | Yes, cacheable or not |
| Runs when | Platform decides | Parameters change | You call it |
| Can change data | Yes, through its own adapters and forms | No | Yes, with a non-cacheable method |
| Security enforcement | Automatic | You write it in Apex | You write it in Apex |
| Refresh | Automatic across components | `refreshApex` | Call again |
A worked decision
Suppose an account page needs two things: the account's name and phone, and a list of its contacts with a "mark as primary" button.
- The name and phone are one record. Use
getRecordfrom LDS. - The contact list is a filtered query over many records. Use a wired, cacheable Apex method with
accountId: '$recordId'. - "Mark as primary" changes data. Use an imperative call to a non-cacheable method, then call
refreshApexon the wired contact list so the page shows the new state.
Each part uses the lightest tool that works. Combining them in one component is normal.
Common mistakes
- Marking a method
cacheable=trueso it can be wired, when it also writes data. Salesforce rejects DML in a cacheable method. - Wiring a method to fetch data on a button click. The wire has already run, and you cannot trigger it on demand. Call the method imperatively.
- Assuming Apex enforces permissions by default. LDS does. Apex does so only through
with sharingandWITH USER_MODE, as in the earlier security chapter.
Exercise
A component shows a search box. When the user presses Search, it must return up to 20 matching accounts by name using a SOQL query. Which option fits best, and why?
Show answer
Imperative Apex. A user action starts the call, and the query filters many records, which LDS cannot do. A wire would run when its parameters change, not on a button press. The method can still be cacheable, but it is called as a function that returns a promise.