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
0
3
8
2
Users Today : 0
Users Yesterday : 0
Users Last 7 days : 12
Users Last 30 days : 41
Users This Month : 33
Users This Year : 381
Total Users : 382
Views Today :
Views Yesterday :
Views Last 7 days : 45
Views Last 30 days : 79
Views This Month : 70
Views This Year : 715
Total views : 716
Who's Online : 0
Your IP Address : 3.137.214.16
Server Time : 2024-12-23
Powered By WPS Visitor Counter