Header Ads

Header ADS

Way number two call apex method from javascript file

1.  Simple way to communicate with apex controller:

wireMethod.cmp :


<template>
    <lightning-card title="Case Details" icon-name="standard:case">
        <div class="slds-m-around_small">
            <template for:each={records} for:item="item">
                <p key={item.Id}>
                    Name: {item.Subject}<br/>
                    Description: {item.Description}<br/>
                    Status: {item.Status}<br/>
                    Origin: {item.Origin}<br/>
                    Priority: {item.Priority}<br/>
                    -------------------------
                </p>
            </template>
            <template if:true={error}>
                There is a error while fetching the error.
            </template>
        </div>
    </lightning-card>
</template>

wireMethod.js:

import { LightningElement, wire, api } from 'lwc';
import getAllCase from '@salesforce/apex/CaseController.getAllCase';

export default class WireMethod extends LightningElement {

    @api records;
    @api error;
   /* @wire(getAllCase) cases; */
   @wire(getAllCase,{priority:'High'}) /* If we use javascript variable then we have to use '$variable' */
        wiredCases({data,error}){
       if(data){
           this.records = data;
           this.error = undefined;
       }
       if(error){
           this.error = error;
           this.records = undefined;
       }
   }

}

CaseController.apex :


public with sharing class CaseController {
    public CaseController() {

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

No comments

Powered by Blogger.