Saturday, 25 January 2020

Data Retrieval – imperative Apex method call

Hi,
Lightning web components can import methods from Apex classes. The imported methods are functions that the component can call either via @wire or imperatively.

  • Wire a property
  • Wire a function
  • Call a method imperatively

Now our example is how to call a method imperatively. here is the code:
HTML:
<template>
    <lightning-datatable data={accountRecords} key-field="id" columns={columns}></lightning-datatable>
</template>

Javascript:

import { LightningElement,track } from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccountRecordsList';

export default class DataDisplayWithImperative extends LightningElement {
    @track accountRecords;
    @track errors;
    @track columns = [{label:'Name',fieldName:'Name',type:'text'},
    {label:'Industry',fieldName:'Industry',type:'text'},];
    connectedCallback(){
        getAccounts()
            .then(result=>{
                this.accountRecords = result;
            })
            .catch(error=>{
                this.errors = error;
            });
    }
}


Meta File:
---
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__Tab</target>
    </targets>
</LightningComponentBundle>


output:
------

References:


No comments:

Post a Comment

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...