Calling the apex Method using imperative way LWC

Call Apex Using Imperative Method

Things to remember before you call apex method with imperative in LWC.
  • Apex controller method should be static
  • It should annotated with @AuraEnable
IMPERATIVE-CALL-LWC
Let's check the below example component to understand more.

callApexUsingImperativeMethod.html
[<template>
    <lightning-card title="Call apex method using imperative way" icon-name="standard:account">
        <div class="slds-m-around_medium">
            <div class="slds-border_bottom">Calling apex method Imperatively</div>
            <template if:true={accounts}>
                <ol class="slds-list_ordered">
                    <template for:each={accounts} for:item="acc">
                        <li key={acc.Id}>{acc.Name}</li>
                    </template>
                </ol>
            </template>
            <lightning-button label="Show All Accounts" variant="brand" onclick={handleGetAccounts}></lightning-button>
            <br/><br/>
           
            <div class="slds-border_bottom">Calling apex method imperative with params</div>
            <p>Annual Revenue: {revenue}</p>
            <lightning-button label="Show revenue" variant="brand" onclick={handleRevenue}></lightning-button>
        </div>
    </lightning-card> 
</template>]

callApexUsingImperativeMethod.js
[import { LightningElement, api, track } from 'lwc';
import accountDetails from '@salesforce/apex/LWC_Util.accountDetails';
import getAllAccounts from '@salesforce/apex/LWC_Util.getAllAccounts';

export default class CallApexUsingImperativeMethod extends LightningElement {
    @api recordId;
    accounts;
    revenue;
    error;
    
    // Simple way of calling, without parameters
    handleGetAccounts(){
        getAllAccounts()
            .then((data) =>{
                this.accounts = data;
                this.error = undefined;
            })
            .catch((error) =>{
                this.accounts = undefined;
                this.error = error;
            })
    }

    // Imperative calling with parameters
    handleRevenue(){
        accountDetails({accountId: this.recordId})
            .then((data) =>{
                this.revenue = data.AnnualRevenue;
                this.error = undefined;
            })
            .catch((error)=>{
                this.revenue = undefined;
                this.error = error;
            });
    }
}]
callApexUsingImperativeMethod.js-meta.xml
[<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>48.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>]
LWC_Util Apex controller:
[public with sharing class LWC_Util {
    
    @AuraEnabled(cacheable=true)
    public static List<Account> getAllAccounts (){
        return [SELECT Id, Name, AccountNumber, Phone, Industry, Type
                    FROM Account];
    }
    
    @AuraEnabled(cacheable=true)
    public static Account accountDetails (String accountId){
        return [SELECT Id, Name, AccountNumber, Phone, Industry, Type, AnnualRevenue
                    FROM Account
                    WHERE Id=:accountId]; 
    }
}]
also find above code in SWDC World git repo.

COMMENTS

Name

Apex,6,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,JS for LWC,6,Lightning Components,2,lightning Page,1,Lightning Web Components,13,Linux for Windows users,1,LWC,2,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Interview Questions,13,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,Test Class,1,Triggers,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: Calling the apex Method using imperative way LWC
Calling the apex Method using imperative way LWC
Call Apex Using Imperative Method,call apex method with imperative in LWC,Calling apex method Imperatively,Calling apex method imperative with params
https://1.bp.blogspot.com/-oKMqgp8tipA/XyWumWyjQLI/AAAAAAAAAag/EajgoWVO6EovADNRJo9N3gcR3pG4N5bfACLcBGAsYHQ/d/IMPERATIVE-CALL-LWC.JPG
https://1.bp.blogspot.com/-oKMqgp8tipA/XyWumWyjQLI/AAAAAAAAAag/EajgoWVO6EovADNRJo9N3gcR3pG4N5bfACLcBGAsYHQ/s72-c-d/IMPERATIVE-CALL-LWC.JPG
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://swdcworld.blogspot.com/2020/08/calling-apex-method-using-imperative.html
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/2020/08/calling-apex-method-using-imperative.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy