refreshApex in LWC
September 20, 2023
Syntax: import { refreshApex } from ‘@salesforce/apex’;
Apex Class
public class AccountHelper {
@AuraEnabled(cacheable=true)
public static List<Account> getAccountList() {
List<Account> accountList= [SELECT Id, Name, Phone, Email FROM Account LIMIT 10];
return accountList;
}
@AuraEnabled
public static void deleteAccount(String accountId) {
Database.delete(accountId);
}
}
refreshApexLWC.html
<template>
<lightning-card title="Account List" icon-name="standard:account">
<template if:true={accounts}>
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds table_striped">
<thead>
<tr class="slds-text-heading_label">
<th scope="col">Name</th>
<th scope="col">Industry</th>
<th scope="col">Phone</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<template for:each={accounts} for:item="account">
<tr key={account.Id}>
<td data-label="Name">{account.Name}</td>
<td data-label="Industry">{account.Industry}</td>
<td data-label="Phone">{account.Phone}</td>
<td data-label="Actions">
<lightning-button-icon icon-name="utility:delete" variant="border-filled"
alternative-text="Delete"
title="Delete"
onclick={handleDelete}
data-account-id={account.Id}></lightning-button-icon>
</td>
</tr>
</template>
</tbody>
</table>
</template>
<template if:true={error}>
<!-- Handle error display -->
</template>
</lightning-card>
</template>
refreshApexLWC.js
import { LightningElement, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getAccounts from '@salesforce/apex/AccountHelper.getAccounts';
import deleteAccount from '@salesforce/apex/AccountHelper.deleteAccount';
export default class RefreshApexLWC extends LightningElement {
@track accounts;
@track error;
@track wiredAccountResult;
@wire(getAccounts)
wiredAccounts( result ) {
this.wiredAccountResult=result;
if (result.data) {
this.accounts = result.data;
this.error = undefined;
} else if (result.error) {
this.accounts = undefined;
this.error = result.error;
}
}
handleDelete(event) {
const accountId= event.target.dataset.accountId;
deleteAccount({ accountId })
.then(() => {
// Perform any success actions if needed
refreshApex(this.wiredAccountResult);
})
.catch((error) => {
// Handle the error if deletion fails
});
}
}
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Inline Feedbacks
View all comments
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
1
1
3
8
Users Today : 1
Users Yesterday : 1
Users Last 7 days : 14
Users Last 30 days : 70
Users This Month : 66
Users This Year : 225
Total Users : 1138
Views Today : 1
Views Yesterday : 2
Views Last 7 days : 16
Views Last 30 days : 113
Views This Month : 108
Views This Year : 346
Total views : 1933
Who's Online : 0
Your IP Address : 216.73.216.134
Server Time : April 30, 2026 1:26 pmPowered By WPS Visitor Counter