Access sObject Values With Static Schema Definition in Apex
Hello Guys, Hope everybody is taking care of themselves and doing great! In this blog, we will be going to learn how to access records of an sObject using static schema. We will call an apex method and access sObject values using a static schema definition.
Example- In this example , we are going to access Name, Title, Email and Phone field from contact object. So let's start doing this-
First we are going to create Aura Enabled method to get Name, Title, Email and phone field from contact.
ContactController.cls
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static Contact getSingleContact() {
return [
SELECT Id, Name, Title, Phone, Email
FROM Contact
LIMIT 1
];
}
}
apexStaticScemaExample.html
<template>
<lightning-card title="ApexStaticSchemaExample"
icon-name="custom:custom63">
<template if:true={contact.data}>
<div class="slds-m-around_medium">
<p>Name : {name}</p>
<p>Title : {title}</p>
<p>Email :
<lightning-formatted-email
value={email}
></lightning-formatted-email>
</p>
<p>Phone : {phone}</p>
</div>
</template>
<template if:true={contact.error}>
<p> errors={contact.error}> </p>
</template>
</lightning-card>
</template>
The above code is used just to show the value of fields on screen.
Name, title ,email, phone are the getter methids in the js file , which are returnig the corresponding value from the contact property that have been populated by wire service.
apexStaticScemaExample.js
import { LightningElement, wire } from 'lwc';
import { getSObjectValue } from '@salesforce/apex';
import getSingleContact from
'@salesforce/apex/ContactController.getSingleContact';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import TITLE_FIELD from '@salesforce/schema/Contact.Title';
import EMAIL_FIELD from '@salesforce/schema/Contact.Email';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
export default class ApexStaticSchemaExample extends LightningElement {
@wire(getSingleContact) contact;
get name() {
return this.contact.data
? getSObjectValue(this.contact.data, NAME_FIELD)
: '';
}
get title() {
return this.contact.data
? getSObjectValue(this.contact.data, TITLE_FIELD)
: '';
}
get email() {
return this.contact.data
? getSObjectValue(this.contact.data, EMAIL_FIELD)
: '';
}
get phone() {
return this.contact.data
? getSObjectValue(this.contact.data, PHONE_FIELD)
: '';
}
}
import { getSObjectValue } from '@salesforce/apex';
It is used as we are using standard getSObjectValue method to get the value of a field from a sObjects record , so we need to import it from the apex folder.
import getSingleContact from '@salesforce/apex/ContactController
.getSingleContact';
This is used to access the aura enabled method from the ContactController apex class.
import NAME_FIELD from '@salesforce/schema/Contact.Name';
Used to import references of field from object.
Notes-
If a component gets an object via an Apex class and imports references to the object from
@salesforce/schema, call getSObjectValue() to get a field value from the object.Now Let's make this component available on App Page, Home Page and Record Page.
apexStaticScemaExample.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Output-
Thanks for reading and happy learning😃.
Stay tuned for next Post.
Keep sharing , keep learning.
CS- D G.
No comments:
Post a Comment