Hey guys, today in this post we are going to learn about How to Call the Apex Method and retrieve list of records using (Imperatively and Wire Service) in LWC – Lightning Web Component.
Call Apex Methods Imperatively →
To control when the method invocation occurs (for example, in response to clicking a button), call the method imperatively. When you call a method imperatively, you receive only a single response. Compare this behavior with @wire, which delegates control to the framework and results in a stream of values being provisioned.
In the following scenarios, you must call an Apex method imperatively as opposed to using @wire.
- To call a method that isn’t 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 aren’t supported by User Interface API, like Task and Event.
- To call a method from an ES6 module that doesn’t extend LightningElement
If an Apex method is marked with @AuraEnabled(cacheable=true), a client-side Lightning Data Service cache is checked before issuing the network call to invoke the Apex method on the server. To know more about Imperatively service, Click Here →
Wire Apex Methods in LWC →
Lightning web components use a reactive wire service. Use @wire in a component’s JavaScript class to specify an Apex method. You can @wire a property or a function to receive the data. To operate on the returned data, @wire a function.
To use @wire to call an Apex method, annotate the Apex method with @AuraEnabled(cacheable=true). A client-side Lightning Data Service cache is checked before issuing the network call to invoke the Apex method on the server. To refresh stale data, call refreshApex(), because Lightning Data Service doesn’t manage data provisioned by Apex. To know more details about Wire Apex Methods, Click Here →
Files we used to Imperatively and Wire Service in Lightning Component →
lwcImperativeWire.html | LWC HTML File | Template HTML file to fetch the records uses of Imperatively and Wire service in (LWC) |
lwcImperativeWire.js | LWC JavaScript File | In the javascript file we import the @salesforce/apex/ library in Salesforce LWC |
lwcImperativeWire.css | Style CSS | It is used to fixed the alignment of the data table as padding and border in lwc component. |
lwcImperativeWire.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
lwcImperativeWireApp.app | Lightning Application | It is used to call the component to preview on browser. |
Final Output →
You can download file directly from github by Click Here.
As per student's demands, I re-opened my eBook of "Salesforce Tutorial" Limited-time huge discount offer Absolutely 50% Off.
I am thinking to give you discount offer occasion of Gandhi Jayanti for a powerful "Salesforce admin course" where you can Understand from Basic Concepts to advanced label in Salesforce.
👉 So Don't MISS it... (Access Right Now)
👉 Get Huge Discount Offer 50%: - Get eBook
Other related post that would you like to learn in Salesforce
- Find the below steps ▾
Create Lightning Web Component HTML →
Step 1:- Create Lightning Web Component : lwcImperativeWire.html
SFDX:Lightning Web Component >> New >> lwcImperativeWire.html
lwcImperativeWire.html [Lightning Web Component HTML]
<template>
<div class="slds slds-p-around_large">
<!--Start Imperative method-->
<div style="text-align:center;"><lightning-button variant="brand" label="Get Account" onclick={accountsAction}></lightning-button></div>
<br/>
<lightning-card icon-name="standard:account" title="Using Imperative method to retrieve Account records">
<table class="tblColPad" border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse;">
<thead>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Industry</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<template for:each={accList} for:item="accItem">
<tr key={accItem.Id}>
<td>{accItem.Name}</td>
<td>{accItem.Phone}</td>
<td>{accItem.Industry}</td>
<td>{accItem.Type}</td>
</tr>
</template>
</tbody>
<tfoot>
<tr>
<td colspan="4"><a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">[To get more examples of lwc..]</a></td>
</tr>
</tfoot>
</table>
</lightning-card>
<!--Close Imperative method-->
<br/><br/>
<!--Start @wire decorator-->
<lightning-card icon-name="standard:lead" title="Using @wire decorator to retrieve Lead records">
<table class="tblColPad" border="1" cellpadding="5" cellspacing="5" style="border-collapse:collapse;">
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Phone</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<template for:each={leadList} for:item="leadItem">
<tr key={leadItem.Id}>
<td>{leadItem.Name}</td>
<td>{leadItem.Title}</td>
<td>{leadItem.Phone}</td>
<td>{leadItem.Email}</td>
</tr>
</template>
</tbody>
<tfoot>
<tr>
<td colspan="4"><a href="https://www.w3web.net/tutorial/salesforce-lwc/" target="_blank" rel="noopener noreferrer">[To get more examples of lwc..]</a></td>
</tr>
</tfoot>
</table>
</lightning-card>
<!--Close @wire decorator-->
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</template>
Create Lightning Web Component JavaScript →
Step 2:- Create Lightning Web Component : lwcImperativeWire.js
SFDX:Lightning Web Component >> New >> lwcImperativeWire.js
lwcImperativeWire.js [LWC JavaScript File]
import { LightningElement,track,wire,api } from 'lwc';
import getLeadList from '@salesforce/apex/lwcImperativeWireCtrl.getLeadList';
import getAccountList from ' lwcImperativeWireCtrl.getAccountList';
export default class LwcImperativeWire extends LightningElement {
// Using imperative call to server
@track accList ;
accountsAction(){
getAccountList().then(result => {
console.log('result#'+result);
this.accList = result;
})
.catch(error => {
console.log('error#'+error);
})
}
// Using @wire decorator call to server
@track leadList ;
@wire (getLeadList)
contactList({error,data}){
if(error){
console.log('error is#'+error);
}
else if(data){
console.log('lead data#'+data);
this.leadList = data;
}
}
}
Create Lightning Web Component Meta XML →
Step 3:- Create Lightning Web Component : lwcImperativeWire.js-meta.xml
SFDX:Lightning Web Component >> New >> lwcImperativeWire.js-meta.xml
lwcImperativeWire.js-meta.xml [LWC Meta Data XML]
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>45.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Style CSS →
Step 4:- Create Style CSS : lwcImperativeWire.css
SFDX:Lightning Web Component >> New >> lwcImperativeWire.css
lwcImperativeWire.css [Style CSS]
table.tblColPad th{background:#ddd;}
table.tblColPad th, table.tblColPad td {padding:5px !important;}
Create Apex Class Controller →
Step 5:- Create Apex Controller : lwcImperativeWireCtrl.cls
SFDX:Create Apex Class >> New >> lwcImperativeWireCtrl.cls
lwcImperativeWireCtrl.cls [Apex Class]
public WITH sharing class lwcImperativeWireCtrl {
// USING Imperative method CALL TO server
@AuraEnabled
public static List<Account> getAccountList(){
List<Account> accList = [SELECT Id, Name, Phone, Industry, TYPE FROM Account LIMIT 10];
RETURN accList;
}
// USING @wire decorator CALL TO server
@AuraEnabled(cacheable=TRUE)
public static List<Lead> getLeadList(){
List<Lead> leadList = [SELECT Id, Name, Title, Phone, Email FROM Lead LIMIT 10];
RETURN leadList;
}
}
Create Lightning Application →
Step 6:- Create Lightning Application : lwcImperativeWireApp.app
From Developer Console >> File >> New >> Lightning Application
lwcImperativeWireApp.app [Component Application File]
<aura:application extends="force:slds">
<c:lwcImperativeWire/>
</aura:application>
Further post that would you like to learn in Salesforce
How do you call Apex class imperatively in LWC?
To call the apex method imperatively, we need to import the method from @salesforce/apex/ module in variable accounts. Then we can use this variable to call the method and pass the parameters if required. This method will return the Promise.
How do I call LWC from Apex?
To call it from Wire Service, the method should be cacheable. Hence, add cacheable=true in @AuraEnabled. And in the LWC js controller, we need to write the import method using the @salesforce/apex/className.
How do you call an Apex class from a custom button in Salesforce?
To call an Apex class from custom button or link on the object detail page, create a VisualForce page and call the Apex class method via the action attribute to make it work. Following is some sample code showing how to do that. The action method invoked when this page is requested by the server.
Related Topics | You May Also Like
- My Udemy Course →



