Introduction

The main concept included in this recipe is using the getrecord wire adapter to retrieve a user 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 adapter belongs to the 'lightning/uiRecordApi' module. The accepted parameters are:

<aside> 💡 For a full explanation on the parameters, check this page getRecord - Salesforce Lightning Component Library.

</aside>

The results are returned to dataand error properties.

Template file

The template shows value of 3 properties: Id, Name and Email :

  1. Id: is retrieved by an import explained in the Javascript section, which is the record id of the logged-in user.
  2. name: is retrieved using a getter function that uses the getFieldValue adapter.
  3. email: is also retrieved using a getter function that uses the getFieldValue adapter.

Using the same approach, errors are shown when the property user.error has any value and user.data is empty, using the new syntax elseif.

...	
	<template lwc:if={user.data}>
      <div class="slds-var-m-around_medium">
          <p>Id: {userId}</p>
          <p>Name: {name}</p>
          <p>
              <lightning-formatted-email
                  value={email}
              ></lightning-formatted-email>
          </p>
      </div>
  </template>
  <template lwc:elseif={user.error}>
      <c-error-panel errors={user.error}></c-error-panel>
  </template>
...

Javascript file

In the import section, there are 3 different modules imported:

//This import is providing the wire adapters
import { getRecord, getFieldValue } 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 worth noting the usage of import Id from '@salesforce/user/Id' to import the current logged user's Salesforce Id. By importing Id from this module, you can access the Id of the current user and use it within your LWC.