Concept and mechanism
Before creating Apex for an LWC, evaluate base components and Lightning Data Service for supported objects and operations. UI API enforces object, field, and record security. Wire provides a stream of values that should be treated as immutable and can emit again when dependencies change.
A display transformation should use a local copy, preserving data ownership. Apex methods used with wire require cacheable true and mutation-free reading. A deliberate write, such as confirming a decision, needs an appropriate path, often an imperative call.
This returns a Promise whose success or failure must be handled explicitly.
Guided application
A confirmed write does not automatically refresh every read path. For Apex wire, retain the provisioned result and pass it to refreshApex; the refresh Promise’s resolved value is not the data array to display. When imperative Apex changes records consumed by LDS, notify affected IDs through the appropriate mechanism.
Do not assume a fixed TTL or repeat the business decision to force visual refresh. Between components, transmit a small contract: a CustomEvent with a primitive ID may suffice to communicate selection. If an object is needed, avoid handing out a mutable reference to the sender’s internal data.
Distinguish pending, completed, and failed states in the operator experience.
Apex for LWC, Flow and Agentforce actions
Another official objective connects Apex to user-interface components, Flow, and Agentforce. When an LWC or an agent action asks Apex to change records, distinguish intent, authorization, and confirmed effect. Define explicit input and output contracts, validate parameters on the server, handle errors deliberately, and check the acting identity and permissions.
Hiding a client-side button is not server authorization; exposing an action to an agent does not waive sharing or data-security checks. For operations with external effects or retries following a timeout, document how to inspect state and prevent duplicate operations.
Test successful paths, denied access, invalid data, and repeated execution. Preserve observability to distinguish a generated response from a confirmed transaction. These principles are taught through independent examples without claiming a paid Salesforce org, live agent configuration, or practical agent execution.
A console displays an Apex-wired list and an LDS panel. After confirming a decision through Apex, it refreshes both consumers through their respective mechanisms. While rereading, it shows a pending state without saving again.
Common pitfalls
Mutating wired data; writing in a cacheable method; unhandled Promises; treating JavaScript Map as a guaranteed serializable parameter; one refresh assumed to cover every path.
Related topics: Data model, identity, and metadata · Collections and choosing configuration or code · Queries, DML, and error outcomes
Distinguish reactive reading, deliberate writing, and cache refresh. Communicate between components through small, clear contracts.
References
- Client-Side Caching of Apex Method Results · Salesforce rolling primary documentation inspected 2026-10-01; API context stated per example
- Lightning Data Service · Salesforce rolling primary documentation inspected 2026-10-01; API context stated per example
- Understand the Wire Service · Salesforce rolling primary documentation inspected 2026-10-01; API context stated per example
- Call Apex Methods Imperatively · Salesforce rolling primary documentation inspected 2026-10-01; API context stated per example
- Create and Dispatch Events · Salesforce rolling primary documentation inspected 2026-10-01; API context stated per example
- Salesforce Certified Platform Developer Exam Guide · Official guide states Summer25; inspected 2026-10-01