Call Apex Class from LWC
September 20, 2023
Call Apex Class from LWC – wire & imperative method
1. Wire a property
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;
}
}
accountListLWC.html
<template>
<lightning-card title="Call Apex Class using Wire">
<div class="slds-m-around_medium">
<template if:true={accounts}>
<template for:each={accounts} for:item="acc">
<p key={acc.Id}>{acc.Name}</p>
<p key={acc.Id}>{acc.Email}</p>
</template>
</template>
</div>
</lightning-card>
</template>
import { LightningElement, wire } from 'lwc';
import getAccountList from '@salesforce/apex/AccountHelper.getAccountList';
export default class AccountListLWC extends LightningElement {
@wire(getAccountList) accounts;
}
2. Wire a function
- It take care of the entire life cycle of an attribute when it changes, including fetching data by calling the method, updating the attribute and refreshing the UI, all in often a single line of code.
- Apex class must have to be cacheable = true.
- On change of a attribute you can call @wire method, you need to use ‘$’ sign before the attribute
Apex Class:
public class AccountHelper {
@AuraEnabled(cacheable=true)
public static List<Account> getAccountList(String accountName) {
List<Account> accountList= [SELECT Id, Name, Phone, Email FROM Account WHERE Name like '%'+accountName+'%' LIMIT 10];
return accountList;
}
}
accountListLWC.html
<template>
<lightning-card title="Wire Method With Parameters">
<lightning-input label="Enter Account Name" type="search" onchange={searchAccount} value={searchKey}></lightning-input>
<br/>
<div class="slds-m-around_medium">
<template if:true={accounts}>
<template for:each={accounts} for:item="acc">
<p key={acc.Id}>{acc.Name}</p>
<p key={acc.Id}>{acc.Email}</p>
</template>
</template>
</div>
</lightning-card>
</template>
accountListLWC.js
import { LightningElement, wire,track } from 'lwc';
import getAccountList from '@salesforce/apex/AccountHelper.getAccountList';
export default class AccountListLWC extends LightningElement {
@track searchKey = '';
@track accounts;
@track error;
searchAccount(event){
this.searchKey = event.target.value;
}
@wire(getAccountList, {accountName:'$searchKey'})
wiredAccounts({data, error}){
if(data){
this.accounts = data;
this.error = undefined;
}
else if (error) {
this.error = error;
this.accounts = undefined;
}
}
}
3- Call a method imperatively >>> call an Apex method on demand
- To call a method that is not annotated with cacheable=true, which includes any method that inserts, updates, or deletes data.
- To control when the invocation occurs.
- To work with objects that are not supported by User Interface API, like Task and Event.
- To call a method from an ES6 module that doesn’t extend LightningElement
Apex Class:
public class AccountHelper {
@AuraEnabled
public static List<Account> getAccountList(String accountName) {
List<Account> accountList= [SELECT Id, Name, Phone, Email FROM Account WHERE Name like '%'+accountName+'%' LIMIT 10];
return accountList;
}
}
accountListLWC.html
<template>
<lightning-card title="Imperative Method With Parameters">
<lightning-input label="Enter Account Name" type="search" onchange={handleChange} value={searchKey}></lightning-input>
<lightning-button label="Search" onclick={searchAccount} ></lightning-button>
<br/>
<div class="slds-m-around_medium">
<template if:true={accounts}>
<template for:each={accounts} for:item="acc">
<p key={acc.Id}>{acc.Name}</p>
<p key={acc.Id}>{acc.Email}</p>
</template>
</template>
</div>
</lightning-card>
</template>
accountListLWC.js
import { LightningElement, track } from 'lwc';
import getAccountList from '@salesforce/apex/AccountHelper.getAccountList';
export default class AccountListLWC extends LightningElement {
@track accounts;
@track error;
@track searchKey;
handleChange(event){
this.searchKey=event.target.value;
}
searchAccount() {
getAccountList({ accountName: this.searchKey })
.then(result => {
this.accounts = result;
})
.catch(error => {
this.error = error;
});
.finally(() => {
console.log('Finally');
});
}
}
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
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
4
4
4
Users Today : 1
Users Yesterday : 6
Users Last 7 days : 142
Users Last 30 days : 282
Users This Month : 156
Users This Year : 531
Total Users : 1444
Views Today : 1
Views Yesterday : 22
Views Last 7 days : 439
Views Last 30 days : 763
Views This Month : 473
Views This Year : 1142
Total views : 2729
Who's Online : 0
Your IP Address : 216.73.216.231
Server Time : June 14, 2026 7:10 pmPowered By WPS Visitor Counter

