EP-09 | Get APEX data with IMPERATIVE METHODS in LWC | LWC Stack ☁️⚡️

 

LWC Stack is Lightning Web Component tutorial series by Salesforce MVP Kapil Batra. In this series you will find LWC tutorials from beginner to intermediate level.

So if you are working on it or planning to learn LWC then this video series is for you. Let's learn and grow together ! Please check complete code below from LWC Stack EP-09

HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<template>
    <lightning-card title="Wired using Imperative Method" icon-name="utility:user">
        <div class="slds-var-m-around_medium">
            <lightning-button label="Get Accounts" onclick={buttonClick}>

            </lightning-button>
        </div>
        <template if:true={accounts}>
            <div class="slds-var-m-around_medium">
                <template for:each={accounts} for:item="acc">
                    <lightning-layout key={acc.Id} class="slds-var-m-vertical_x-small">
                        <lightning-layout-item flexibility="grow">
                            {acc.Name}
                        </lightning-layout-item>
                        
                    </lightning-layout>
                </template>
            </div>
            <div class="slds-var-m-around_medium">
                <br/>
                {error}
            </div>
        </template>
    </lightning-card>
</template>
JavaScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import { LightningElement } from 'lwc';
import getAccountList from '@salesforce/apex/AccountController.getAccList';

export default class BindImperativeMethod extends LightningElement {
    accounts;
    error;

    buttonClick(){
        getAccountList()
        .then((result) =>{
            this.accounts = result;
            this.error = undefined;
        })
        .catch((error)=>{
            this.error = error;
            this.accounts = undefined;
        });

    }
}

Output

Checkout complete tutorial below :

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments