Concepts or coding lessons of Salesforce that you can implement easily

2 ways to call Apex method using @wire in LWC

 In this blog, we are going to see 2 way to call apex class method using @wire in LWC.


Create Apex class : CaseController

public with sharing class CaseController {
    @AuraEnabled(cacheable= true)
    public static List<Case> getAllCases(){
        try {
            List<Case> lstCase = [SELECT Id, Subject, Origin FROM Case];
            return lstCase;
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}

1 : @wire(ApexMethodName) variableName;

Create Lightning web component : wireMethod.html

<template>
    <lightning-card title="All Cases" icon-name="standard:case" variant="base">
      <div>
        <!-- Card Body  -->
        <template for:each={caseList.data} for:item="objItem">
            <p key={objItem.Id}>
                {objItem.Subject} <br/> {objItem.Origin}
            </p>
        </template>
        <template if:true={caseList.error}>
            Something goes wrong.
        </template>
      </div>
    </lightning-card>
</template>

wireMethod.js

import { LightningElement, wire } from 'lwc';
import getAllCases from '@salesforce/apex/CaseController.getAllCases';
export default class WireMethod extends LightningElement {

    /*
        @wire(getAllCases) caseList;
        caseList have 2 parameters
        - data : returned data by apex
        - error : contains error if any
    */
   
    @wire(getAllCases) caseList;
}


2 : @wire(ApexMethodName) variableName;

wireMethod.html

<template>
    <lightning-card title="All Cases" icon-name="standard:case" variant="base">
      <div>
        <!-- Card Body  -->
        <template for:each={records} for:item="objItem">
            <p key={objItem.Id}>
                {objItem.Subject} <br/> {objItem.Origin}
            </p>
        </template>
        <template if:true={errors}>
            Something goes wrong.
        </template>
      </div>
    </lightning-card>
</template>

wireMethod.js

import { LightningElement, wire, api } from 'lwc';
import getAllCases from '@salesforce/apex/CaseController.getAllCases';
export default class WireMethod extends LightningElement {
    @api records;
    @api errors;
    /*
        @wire(getAllCases)
            wiredCase ({ data, error }){
                - data => return list of case
                - error => error in for, of object
            }
    */
   @wire(getAllCases)
            wiredCase ({
                data, error
            }){
                if(data){
                    this.records = data;
                    this.errors = undefined;
                }
                if(error){
                    this.records = undefined;
                    this.errors = error;
                }
            }
}


Subscribe blog for More updates !!! 


Read More: WhatsApp on Salesforce Integration in 5 Steps
                    Which Salesforce API Do I Use in Integration?
                    When To Use Salesforce REST API  
                   When To Use Salesforce SOAP API 
                   When To Use Salesforce Bulk API 


Latest Salesforce Interview Questions and Answers: