The independent daily for Salesforce admins, developers and architects

Curated by Abhinav (opens in a new tab)

Five ways Lightning Web Components can share data between components (opens in a new tab)

You will see how a parent passes data to a child, how a child reports back, and when to use Lightning Message Service or Lightning Data Service instead.

Why read this

The piece also lists common mistakes to avoid.

Parent to child

  • @api public property
  • Parent sets HTML attribute
  • Child reads the value

Child to parent

  • Child dispatches CustomEvent
  • Detail carries the payload
  • Parent handles the event
Parent-to-child uses public properties; child-to-parent uses custom events

The article surveys five ways Lightning Web Components can communicate. The simplest is parent-to-child communication through a public property marked with the @api decorator, where the parent sets an HTML attribute and the child receives it. Code examples show a child component declaring a message property and a parent passing a welcome string into it.

When a child needs to notify its parent, it dispatches a custom event carrying a detail payload. The example has a child button fire a valuechange event, and the parent handles it and stores the selected value. The author notes that this lets the child report upward without reaching into the parent’s internal properties.

For components with no parent-child relationship, such as a customer information panel and a related sales panel, the article describes Lightning Message Service. The steps are to create a message channel, import it into a publisher, publish a message, subscribe in the receiving component and process the payload. Lightning Data Service is presented for cases where the data is a Salesforce record, using getRecord through a wire adapter, with the benefit of caching and record sharing across components. Shared JavaScript modules are mentioned for reusable logic.

The article closes with a table matching each method to its best use and a list of mistakes to avoid, such as using custom events where a public property is enough, creating unnecessary message channels, and calling Apex when Lightning Data Service already covers the need.

Read the original at dev.to (opens in a new tab)

Worth reading?

Must-read

Seven bugs that passed a 113-test Salesforce suite and the fix for each (opens in a new tab)

You will learn seven ways Apex and LWC code can pass its tests yet fail in a real org, including keyset paging, retried saves, restricted picklists and cacheable methods.

Why read this

Each comes with a concrete fix and a check you can run on your own code.

Recursion guard (bug 2)

  • Static Id set skips retried records
  • Silent skip with all-or-none off
  • Compare old and new values instead

Cacheable refresh (bug 5)

  • cacheable=true returns stale rows
  • Mocked Jest tests cannot catch it
  • Drop cacheable or use refreshApex()
The author's fixes for two of the seven bugs, set side by side

The author’s starter kit had 113 passing tests, 68 Apex and 45 Jest, and deployed cleanly to a real org. A tester clicking through the data table and a reviewer reading the code together found seven bugs that no test had caught. The gaps lay between what the tests checked and what a live org did.

The first bug was a paged table that kept showing page one. The server used a keyset cursor, and a cached browser component sent only a page number, so the server treated every request as page one. The tests called each version with the arguments it expected, so an old client against a new server was never tested. The fix is to fall back to offset paging when a later page arrives without a cursor, and to check what an old client would receive after changing an @AuraEnabled contract. The second bug was a static Id-based recursion guard that silently skipped retried records when a save ran with all-or-none off. The fix is to compare old and new values and recalculate for work that is safe to repeat, keeping an Id guard only for work that must happen exactly once.

The third bug was a logger whose Platform Event carried any LoggingLevel name, while the Log__c picklist allowed only ERROR, WARN, INFO and DEBUG. Logs finer than DEBUG failed silently in the subscriber trigger. The fix is to store anything finer as DEBUG, and to test every restricted picklist with a value outside its list. The fourth was an example Account trigger shipped switched on, which changed the data of anyone who deployed the kit. Example code should ship disabled, and the tests should enable handlers only while they run.

The fifth bug was a refresh() method that could show stale rows, because the backing Apex method was cacheable. Jest mocks cannot catch this, so it appears only in a real browser. The fix is to drop cacheable=true where current data matters, or to use @wire with refreshApex(). The sixth was a no-CLI zip, built by hand, that still held six older files. The fix is to build the zip from the source folder in one script, compare every file, and deploy the zip as the final test. The seventh was an offset calculation that overflowed for very large page numbers before the row-limit check ran. The fix is to compare before multiplying.

The author groups the bugs into gaps between versions, between code and platform, between the developer’s org and the buyer’s, and at input edges nobody types on purpose. The closing advice is that passing tests cover only what the developer thought of, so manual testing and fresh review are needed before calling code production-ready.

Read the original at dev.to (opens in a new tab)

Worth reading?

How LWR differs from Aura for Experience Cloud sites and what migration involves (opens in a new tab)

You will learn how Lightning Web Runtime delivers pages differently from Aura, where the performance gains come from and where they do not, and what an Aura-to-LWR migration usually involves.

Why read this

The piece helps you decide whether a new or existing site should move to LWR.

Aura

  • Components delivered dynamically
  • No republish for component changes
  • Broader existing template set

LWR

  • Static, cacheable resources
  • CDN can serve eligible files
  • Republish to expose component changes
LWR publishes cacheable resources, while Aura delivers components dynamically

Lightning Web Runtime (LWR) is the newer runtime behind Experience Cloud templates such as Build Your Own (LWR) and Microsite (LWR). Components are built with Lightning Web Components, and the site publishes prepared, cacheable resources that can be served through a content delivery network. Aura delivers components dynamically, so LWR can load faster, but the article stresses that speed is not guaranteed. Large images, unnecessary API calls and poorly designed custom components still slow pages, and integration changes may matter more than the storefront technology.

The main practical differences are in publishing and component support. Aura sites can pick up component code changes without republishing, while LWR sites must be republished for updated components to reach visitors. Standard LWR templates use LWC only, and LWR offers fewer templates and components than Aura, so some Aura features have no direct equivalent. Salesforce recommends LWR for new B2B Commerce storefronts and encourages migration from Aura-based B2B stores.

Migrating an Aura site usually means replacing Aura components with LWC, while many existing LWCs can be reused with few changes. The article advises inventorying components and dependencies, transferring storefront configuration, and testing the full user journey. It also recommends recording baseline performance with tools such as Lighthouse, WebPageTest and Core Web Vitals before and after migration.

Read the original at dev.to (opens in a new tab)

Worth reading?

Must-read

Avoiding runtime anti-patterns in Lightning Web Components (opens in a new tab)

You will learn why global DOM queries, ID selectors, setTimeout-based timing and early lifecycle access break LWC components, and the reliable alternatives for each.

Why read this

The piece closes a series, so the reader gets a complete set of runtime fixes.

Fragile timing workaround

  • setTimeout to wait for data
  • setTimeout(..., 0) after a property change
  • Guessed wall-clock delay

Reliable alternative

  • async and await for loaded data
  • renderedCallback after each render
  • Signal from the engine
Replacing timing workarounds with lifecycle-aware code makes updates reliable

This final post in a series on Lightning Web Component anti-patterns covers runtime mistakes in DOM access, asynchronous timing, the component lifecycle, performance, and base component usage. The article’s central point is that these bugs stem from wrong assumptions about when things happen, such as when the DOM exists, when a render has completed, and which runtime a component runs in.

On DOM access, the article warns against querying the global document, since Lightning Web Security scopes calls to the sandbox and tests may behave differently from production; it recommends scoping queries to the component’s template or using LWC:ref. It also warns against setting innerHTML, which carries XSS risk and conflicts with reactive rendering, recommending declarative markup and lightning-formatted-rich-text for trusted rich text. ID selectors are unreliable because LWC rewrites id attributes at render time, so class selectors, data attributes, or LWC:ref should be used instead. For timing, setTimeout should not be used to wait for async data (use async/await) or for re-renders (use renderedCallback, which runs after every render). For lifecycle, @api values are not readable in the constructor, DOM access fails in the constructor and connectedCallback because nothing has rendered yet, and this.template.children should be avoided in favour of stable selectors.

Performance guidance says that repeatedly crossing the Lightning Web Security proxy boundary becomes costly with large data: clone host data into local arrays once, push aggregation into Apex, and prefer plain objects and arrays over Set and Map for large collections. The base component section covers several silent failures: platformShowToastEvent does nothing outside Lightning Experience and mobile, so lightning/toast should be used; only lightning/modal and lightning/datatable support extension, so other components should be wrapped; the record ID should be read from the success event rather than from submit; internal SLDS shadow styles should not be overridden, and the public –slds- hooks used instead; and mobile support must be checked, since lightning-datatable fails silently in the Salesforce Mobile App and desktop responsive mode does not reliably reproduce this.

The conclusion ties the patterns together: LWC guarantees are narrower than they seem, and assuming readiness before it exists causes most of these problems. The article closes with a list of resources, including lifecycle documentation, LWC:ref, lightning/toast, @Salesforce/client/formFactor, and the LWC ESLint plugin rules.

Read the original at Salesforce Developers Blog (opens in a new tab)

Worth reading?

Salesforce announces general availability of Web Console as a modern browser-based IDE (opens in a new tab)

Readers will learn how to access and use the new Web Console for inline Apex and Lightning Web Component editing, debugging, and query execution directly inside their org.

Why read this

The article explains the production guardrails, Setup steps, and how it differs from the legacy Developer Console and Agentforce Vibes IDE.

Salesforce has released Web Console as a generally available browser-based integrated development environment. Built on VS Code for the Web and the existing Salesforce extensions, it serves as a modern replacement for the legacy Developer Console while maintaining platform safety standards.

The tool supports core development tasks including inline editing of Apex classes and triggers, viewing and modifying Lightning Web Component files, running anonymous Apex scripts, and executing SOQL queries with optional Performance Plans. Developers can also run test suites, manage Trace Flags for debugging, and navigate the entire metadata tree using the Org Browser.

Access controls differ by environment, allowing read-only inspection in production orgs while permitting direct saves in sandboxes. Administrators can enable the feature through the Setup menu, where it is turned on by default. The announcement clarifies that this lightweight single-org editor targets quick troubleshooting and routine edits, distinct from the heavier virtual machine-backed Agentforce Vibes IDE.

Read the original at Salesforce Developers Blog (opens in a new tab)

Worth reading?