Showing Static Record using Apex to Wire Mechanism

Now, we will see how to get Salesforce data as static way using Apex.

We have built a LWC named “showWireApexStaticContact“.

Step 1: We have created an Apex Class named “LWC_ContactController” by which we will send the Contact record to Lightning Web Component. We can use the same class.


public with sharing class LWC_ContactController{
	public static Contact getStaticContact(){
		return [SELECT Id, Name, Title, Phone, Email FROM Contact LIMIT 1];

getStaticContact() method returns the Contact Record.

Step 2: As per previous examples, we need to update the “showWireApexStaticContact.js-meta.xml” file to set the visibility in Lightning App Builder.


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="" fqn="showWireApexStaticContact">

Step 3: Need to write the respective HTML file.


        <lightning-card title="Showing Static Record (Wire * Apex)">            
              <lightning-layout-item flexibility="auto" padding="around-small">
                   <template if:true={}> 
                        <div class="slds-m-around_medium">
                            <p><lightning-formatted-email value={email}></lightning-formatted-email></p>
                    <template if:true={contactObj.error}>

Step 4: Now, we need to work on the JS file.


import { LightningElement, wire } from 'lwc';
import { getSObjectValue } from '@salesforce/apex';
import getStaticContact from '@salesforce/apex/LWC_ContactController.getStaticContact';

import CONTACT_NAME_FIELD from '@salesforce/schema/Contact.Name';
import CONTACT_PHONE_FIELD from '@salesforce/schema/Contact.Phone';
import CONTACT_EMAIL_FIELD from '@salesforce/schema/Contact.Email';

export default class ShowStaticContact extends LightningElement {

    @wire(getStaticContact) contactObj;

    get name(){
            ? getSObjectValue(, CONTACT_NAME_FIELD)
            : '';

    get phone(){
            ? getSObjectValue(, CONTACT_PHONE_FIELD)
            : '';

    get email(){ 
            ? getSObjectValue(, CONTACT_EMAIL_FIELD)
            : '';

We have imported getSObjectValue from ‘@salesforce/apex’ and getStaticContact from our own Apex Class “LWC_ContactController” to get the Contact record.

In all getter functions(, System will find out the specific field from the entire Contact Record by getSobjectValue() function.

Step 5: Need to add this component into Lightning App/Home or Record Page.


8,479 total views, 6 views today

Rating: 5.0/5. From 2 votes.
Please wait...
This entry was posted in Lightning Web Component. Bookmark the permalink.

Leave a Reply