Development questions
What is refreshApex and how do you use it in an LWC?
DevelopmentMedium
refreshApex (from @salesforce/apex) forces a wired Apex method to re-fetch data and update the cache, so the UI shows fresh results after a change.
You must keep a reference to the entire wired response, not just the data:
javascript
import { refreshApex } from '@salesforce/apex';
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredResult = result;
if (result.data) this.contacts = result.data;
}
async handleSave() {
await saveContact({ c: this.contact });
await refreshApex(this.wiredResult);
}
For Lightning Data Service records use notifyRecordUpdateAvailable instead.
lwcrefreshapexwire
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?