EP-11 | Get data using APEX Static Schema in LWC | LWC Stack ☁️⚡️


LWC Stack is Lightning Web Component tutorial series by Salesforce MVP Kapil Batra. In this series you will find LWC tutorials from beginner to intermediate level.

So if you are working on it or planning to learn LWC then this video series is for you. Let's learn and grow together ! Please check complete code below from LWC Stack EP-11

HTML

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<template>
    <lightning-card title="Bind Using APEX Schema" icon-name="utility:user">
        <template if:true={account.data}>
            <div class="slds-var-m-around_medium">
              <b>
                  Name : {name}
                  <br/>
                  Phone : {phone}
              </b>
            </div>
        </template>
    </lightning-card>
</template>
JavaScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import { LightningElement, wire } from 'lwc';
import {getSObjectValue} from '@salesforce/apex';
import getSingleAcc from '@salesforce/apex/AccountController.getSingleAccount';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import PHONE_FIELD from '@salesforce/schema/Account.Phone';

export default class BindUsingApexSchema extends LightningElement {
   @wire(getSingleAcc) account;
   get name(){
       return this.account.data ? getSObjectValue(this.account.data, NAME_FIELD):'';
   }
   get phone(){
    return this.account.data ? getSObjectValue(this.account.data, PHONE_FIELD):'';
}

}
APEX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public with sharing class AccountController{
    @AuraEnabled(cacheable=true)
    public static List<Account> getAccList(){
        return [SELECT Id, Name FROM ACCOUNT ORDER BY CreatedDate desc Limit 10];
    }

    @AuraEnabled(cacheable=true)
    public static List<Account> findAccList(String keyword){
        String key='%'+keyword+'%';
        return [SELECT Id, Name,Phone FROM ACCOUNT WHERE Name LIKE:key ORDER BY CreatedDate desc Limit 10];
    }
    @AuraEnabled(cacheable=true)
    public static Account getSingleAccount(){
        return [SELECT Id, Name, Phone FROM ACCOUNT ORDER BY CreatedDate desc Limit 1];
    }

}
Output


Checkout complete video tutorial below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks

Happy Coding :)

Post a Comment

0 Comments