← Salesforce Platform Developer: Apex and Lightning
05 / 8 · 40 MIN

LWC: data, events, and refreshing reads

Choose data paths and refresh the UI without repeating business operations.

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.

IN PRACTICE

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

Take this idea with you

Distinguish reactive reading, deliberate writing, and cache refresh. Communicate between components through small, clear contracts.

Create account

References

Salesforce is a trademark of Salesforce, Inc. bigsavant.com is an independent preparation platform and is not affiliated with, associated with, sponsored, authorised or endorsed by Salesforce. Content and questions are original, are not official exam questions, and completing our tests does not award or guarantee any certification. Names are used only to identify the subject. All other trademarks belong to their respective owners.