The main concept included in this recipe is using the getrecord
adapter to retrieve record’s data.
<aside>
💡 If you are not familiar with using the @wire
decorator, review this section Using pre-configured adapters with the @wire
decorator.
</aside>
<aside>
📂 Find the LWC in this folder: ****force-app/main/default/lwc/wireGetRecord.
</aside>
The getrecord
belongs to the 'lightning/uiRecordApi'
module. The accepted parameters are basically the record Id and the fields to retrieve using an array.
For a full explanation on the parameters, check this page getRecord - Salesforce Lightning Component Library.
The results are returned to data
and error
properties.
The template shows the value in the property named recordStr
when the property record.data
is not empty.
Using the same approach, errors are shown when the property record.error
has any value and record.data
is empty, using the new syntax elseif
.
...
<div class="slds-var-m-horizontal_medium">
<template lwc:if={record.data}>
<div class="scroller">
<pre>{recordStr}</pre>
</div>
</template>
<template lwc:elseif={record.error}>
<c-error-panel errors={record.error}></c-error-panel>
</template>
</div>
...
In the import
section, there are 3 different modules imported:
//This import is providing the wire adapter
import { getRecord } from 'lightning/uiRecordApi';
//Those 2 imports are statically importing fields from the User object
import NAME_FIELD from '@salesforce/schema/User.Name';
import EMAIL_FIELD from '@salesforce/schema/User.Email';
//This import is refering the the User object also, but in a particular way
import Id from '@salesforce/user/Id';
It is particularly interesting this import Id from '@salesforce/user/Id'
as it is used to import the current logged user's Salesforce Id. By importing Id
from this module, you can access the Salesforce ID of the current user and use it within your LWC.
After the import, the crucial part is invoking the getRecord
wired adapter:
recordId
required parameter assigned to the reactive $userId
fields
and optionalFields
are passed using the statically imported fieldsAssigning the reactive property to the recordId
parameter makes a new every time the userId
changes.
<aside> 📢 If you are not familiar with this notation, please review this section Mark a configuration property as Dynamic and Reactive with $.
</aside>