Salesforce | Name the Apex Methodology Imperatively in Lightning Internet Part?


After we wish to management when a technique ought to be invoked (as an example, in response to a button click on), we use an crucial methodology name. 

We solely get one response after we name a technique imperatively. 

With @wire, alternatively, it cedes authority to the framework and causes the provisioning of a stream of values. 

Annotate the Apex methodology with @AuraEnabled(cacheable=true) if you wish to make the most of @wire to name it. Earlier than making a community name to the server to name the Apex methodology, a client-side Lightning Knowledge Service cache is checked. 

The next conditions require you to contact an Apex methodology immediately, relatively than by way of @wire. 

  • To invoke any methodology that inserts, modifies, or deletes information and is not tagged with cacheable=true. 
  • To manage when the invocation takes place. 
  • To work with objects like Job and Occasion that Person Interface API doesn’t present. 
  • Utilizing an ES6 module’s methodology name that doesn’t lengthen LightningElement 

dont miss out iconRemember to take a look at: Learn All About the Batch Apex in 2023

Earlier than making a community request to the server to name an Apex methodology tagged with @AuraEnabled(cacheable=true), the client-side Lightning Knowledge Service cache is examined. Nevertheless, Apex-provisioned information is just not managed by Lightning Knowledge Service. Consequently, to replace the Lightning Knowledge, execute getRecordNotifyChange() after calling the Apex methodology. 

Allow us to perceive with a easy example- 

ImperativeMethod.html 

<template> 
    <lightning-card title="Imprerative Demo"> 
        <div fashion="top: 200px"> 
            <lightning-datatable 
            key-field="Id" 
            information={information} 
            columns={columns}> 
        </lightning-datatable> 
        </div> 
    </lightning-card>   
</template>

ImperativeMethod.js 

import { LightningElement,monitor } from 'lwc'; 
import  getCntDetails from '@salesforce/apex/ImperativeMethods.getCntDetails'; 
const columns=[ 
    {label:"Contact Id",fieldName:"Id"}, 
    {label:"Contact Name",fieldName:"Name"}, 
    {label:"Account Name",fieldName:"AccountId"}, 
]; 
export default class ImperativeMethod extends LightningElement { 
    @monitor columns=columns; 
    @monitor information=[]; 
    connectedCallback(){ 
        getCntDetails() 
        .then(consequence =>{ 
            this.information=consequence; 
        } 
            ) 
            .catch(error =>{ 
                console.log("error occurd"); 
            }) 
    }

dont miss out iconTry one other superb weblog by Narendra Kumar right here: Navigation Service in Lightning Web Component | All You Need to Know

ImperativeMethod.xml 

<?xml model="1.0" encoding="UTF-8"?> 
<LightningComponentBundle xmlns="http://cleaning soap.sforce.com/2006/04/metadata"> 
    <apiVersion>55.0</apiVersion> 
    <isExposed>true</isExposed> 
    <targets> 
        <goal>lightning__HomePage</goal> 
    </targets> 
</LightningComponentBundle>

ImperativeMethod.cls 

public with sharing class ImperativeMethods { 
    @AuraEnabled(cacheable=true) 
    public static listing<Contact> getCntDetails() { 
        listing<Contact>contactDetails=[select Id,Name,AccountId from Contact]; 
        return contactDetails; 
}
}

OUTPUT 


 





Source link

Thanks for Reading

Enjoyed this post? Share it with your networks.

Leave a Feedback!