Apex Imperative Method

Already, we have studied how to get data from Lightning Platform Database using wire mechanism in Lightning Web Component. Now, we will study how to get data from Lightning Platform Database using Imperative Way in Lightning Web Component.

Step 1: We have created an Apex Class named “LWC_ContactController” by which we will send the Contact record to Lightning Web Component. We can use the same class.


public with sharing class LWC_ContactController{
	publicstaticList<Contact> getImperativeContactList(){
		return [SELECTId, Name, EmailFROMContactLIMIT1];

getImperativeContactList() method of the Apex Class “LWC_ContactController” returns the Contact Record. Please notice that we didn’t use cacheable = true for getImperativeContactList() method just like wiring mechanism. That is the primary difference between wiring mechanism and imperative mechanism.

Step 2: After that, we have built a LWC named “showContactsIM“. As per previous examples, we need to update the “showContactsIM.js-meta.xml” file to set the visibility in Lightning App Builder.


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="showContactsIM">

Step 3: Need to write the respective HTML file.


    <lightning-card title="Apex Imperative Method">            
          <lightning-layout-item flexibility="auto" padding="around-small">
               <template if:true={contacts}>                        
                    <template for:each={contacts} for:item="contact">                        
                        <p key={contact.Id}>                               
                            <lightning-formatted-email value={contact.Email} class="slds-m-left_medium"></lightning-formatted-email>
                <template if:true={error}>

Step 4: Now, we need to work on the JS file.


import { LightningElement, track } from 'lwc';
import getImperativeContactList from '@salesforce/apex/LWC_ContactController.getImperativeContactList';

export default class ShowContactsIM extends LightningElement {
    @track contacts;
    @track error;

    connectedCallback(){ //you can build a method for a button
        .then(result => {
            this.contacts = result;
            this.error = undefined;
            this.error = error;
            this.contacts = undefined;

As per the imperative mechanism writing rules, first we have imported “getImperativeContactList” from the Apex Class, “LWC_ContactController”. Then, we have invoked a method with the same name of adapter which is getImperativeContactList() using javascript promise during component creation(connectedCallback()). We have set the result to the respective reactive properties such as contacts and error.

We didn’t use cacheable = true for getImperativeContactList() method as per imperative mechanism guide line.

Step 5: At last, need to add this LWC into Lightning App/Home or Record Page.


23,178 total views, 3 views today

Rating: 4.6/5. From 5 votes.
Please wait...
This entry was posted in Lightning Web Component. Bookmark the permalink.

Leave a Reply