ForceChallenges
ProblemsDailyNotesInterview
Sign in

Practice

All problemsSOQL practiceApex trigger challengesDaily challengeOpportunityTrigger.oldMapBefore UpdateBulkificationContactValidationGROUP BYSemi-JoinAccountBefore Insert

Interview questions

All questionsAdminAdministrationApexApex TriggerAsync ApexDevelopmentField ServiceField ServiceIntegrationLwc

By company

DeloitteSalesforceAccentureInfosysPwCCognizant

Learn

Study notes

ForceChallenges

AboutContact usPrivacy PolicyTerms of ServiceFor AI assistants (llms.txt)
© 2026 ForceChallenges · Salesforce SOQL & Apex trigger practice
LWC questions43 of 50
LWCMedium 2 min read

What is the purpose of refreshApex, and how do you use it with wired data adapters?

Answer

Basics: Wired Apex (cacheable=true) results are cached client-side for performance, so after you save something, the old list can still show. refreshApex tells the wire to get fresh data. It is imported from @salesforce/apex.

How it works:

  1. Wire to a property or function and keep the entire provisioned object (not just .data).
  2. After a mutation (imperative Apex DML, updateRecord, flow), call refreshApex(this.wiredResult).
  3. It returns a Promise; await it if you need to sequence actions.
  4. The wire re-emits fresh {data, error} and the template re-renders.
  5. When wiring to a function, store the whole argument in a field: this.wiredResult = result.

Advanced / deep dive: The argument must be the exact value the wire provisioned. Passing result.data does nothing. refreshApex only affects wired adapters of cacheable Apex; it does not affect results fetched imperatively, which are your own variables, so re-call the method instead. For Lightning Data Service adapters (getRecord) use notifyRecordUpdateAvailable or getRecordNotifyChange from lightning/uiRecordApi (the newer name replaced the older one in recent releases; verify in current release notes). LDS operations such as updateRecord refresh their own wires automatically, so you only need refreshApex when Apex changed the data. When multiple components wire the same Apex call with the same params they share one cache entry; one refreshApex call refreshes the data for all of them. Be careful with refresh loops where a handler triggers a refresh that triggers the handler. Cache refresh issues a server round trip, so avoid calling it per keystroke; debounce.

Practice SOQL & Apex
PreviousWhat is the Lightning Message Service (LMS), and when should you use it for cross-component communication?Next Explain the concept of Shadow DOM in LWC and how it enforces component encapsulation and styling isolation.

More LWC questions

  • How do you create custom modal dialog windows in LWC using LightningModal?
  • How can developers utilize lightning/uiListApi to fetch and display standard list view data?
  • How do you subscribe to Platform Events and streaming channels in LWC using lightning/empApi?
  • How can developers navigate to object home views and record creation pages using NavigationMixin?
  • How does the LWC reactivity engine manage property observation and virtual DOM updates?

Code / config example:

javascript
import { LightningElement, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getTasks from '@salesforce/apex/TaskController.getTasks';
import completeTask from '@salesforce/apex/TaskController.completeTask';

export default class TaskList extends LightningElement {
    wiredResult;
    tasks;
    @wire(getTasks)
    wired(result) {
        this.wiredResult = result;            // keep whole object
        if (result.data) this.tasks = result.data;
    }
    async handleComplete(e) {
        await completeTask({ taskId: e.target.dataset.id });
        await refreshApex(this.wiredResult);   // re-fetch from server
    }
}

Hidden gotchas & must-remember points:

  • Pass the whole wire result, not data; passing data silently does nothing useful.
  • It refreshes cacheable wired Apex only, not imperative calls.
  • It is imported from @salesforce/apex, not from lightning/uiRecordApi.
  • LDS adapters have their own refresh path via record notification functions.
  • Records changed outside the component (another user) are not pushed; you must refresh or use events.
  • Each call is a network request; do not call it in tight loops.

Best practice: Store the full wire result, refresh after confirmed writes, and prefer LDS operations when Apex is not truly needed.

LWCrefreshApexCaching