Header Ads

Header ADS

Way number three call apex method from javascript file ( Imperative call)

3.  Imperative way to communicate with the apex controller:

wireMethod.cmp :

<template>
    <lightning-card title="Apex Imperative" icon-name="standard:case">
        <div class="slds-m-around_small">
            <lightning-input value={priority}
            label="Search Cases accroding to Priorty"
            onchange={handleChange}
            name="pname"></lightning-input>
        </div>
        <div class="slds-m-around_small">
            <template for:each={records} for:item="item">
                <p key={item.Id}>
                    Case name : {item.Subject}<br/>
                    Case Description: {item.Description}<br/>
                    Case Priority : {item.Priority}<br/>
                    Case Origin : {item.Origin}<br/>
                    Case Status : {item.Status}<br/>
                    --------------------------------------
                </p>
            </template>
        </div>
    </lightning-card>

</template>

wireMethod.js:

import { LightningElement, track } from 'lwc';
import getAllCase from '@salesforce/apex/CaseController.getAllCase';
export default class ApexImperativeMethod extends LightningElement {

    @track records;
    @track error;

    handleChange(event){
        const pValue = event.target.value;
        getAllCase({
            priority: pValue
        })
        .then(result=>{
            this.records = result;
            console.log('Case Records : '+result);
        })
        .then(error=>{
            this.error = error;
            console.log("Error : "+error);
        })
    }
}


CaseController.apex :


public with sharing class CaseController {
    public CaseController() {

    }
    @AuraEnabled(cacheable=true)
    public static List<Case> getAllCase(String priority){
        return [
            Select Subject,Description,Status,Origin,Priority from Case where Priority =: priority
        ];
    }
}

No comments

Powered by Blogger.