How can I obtain the Logged In User's data in LWC on a Salesforce Community Page? – Salesforce

by
Ali Hasan
html lwc oracle-apex salesforce

Quick Fix: Utilize @wire to fetch the Logged In User’s data on a Salesforce Community Page. Pass no parameters to Apex, allowing you to retrieve necessary information server-side.

The Problem:

In a project to display a list of fields from related records on a Salesforce Community Page, the developer is unable to obtain the Logged In User’s data to proceed. Using Wire for this purpose is not feasible due to its incompatibility with Lightning Home Pages. Existing code, despite being an attempt to retrieve User details via an Apex class and a JavaScript function, is not displaying the necessary data. The error faced is ‘List has no rows for assignment to SObject’, highlighting the absence of User data. The goal is to retrieve the Logged In User’s details, such as Name and Account_Id__c, and subsequently retrieve related Location_Data__c records for further processing.

The Solutions:

Solution 1: Using Apex wire

If you’re unable to use @wire on a Lightning Home Page, you can try an alternative approach using Apex wire. This involves making a call to an Apex method to fetch the user’s data, and then storing the result in a JavaScript variable.

Here’s an example of how you can do this:

Apex Class:

public class UserDetailsController {
    @AuraEnabled(cacheable=true)
    public static User getUserDetails() {
        return [SELECT Name, Account_Id__c FROM User WHERE Id = :UserInfo.getUserId() LIMIT 1];
    }
}

JavaScript:

import { LightningElement, wire } from 'lwc';
import getUserDetails from '@salesforce/apex/UserDetailsController.getUserDetails';

export default class UserDetailsCard extends LightningElement {
    @wire(getUserDetails)
    userDetails;

    connectedCallback() {
        // Fetch the user details when the component is loaded.
        this.fetchUserDetails();
    }

    fetchUserDetails() {
        if (this.userDetails.data) {
            this.userName = this.userDetails.data.Name;
            this.accountId = this.userDetails.data.Account_Id__c;
        } else if (this.userDetails.error) {
            console.error('Error fetching user details:', this.userDetails.error);
        }
    }
}

HTML:

<template>
  <lightning-card title="User Details">
      <div class="slds-p-around_medium">
          <p><b>Name:</b> {userName}</p>
          <p><b>Account ID:</b> {accountId}</p>
          <p><b>Errors:</b> {errors}</p>
      </div>
  </lightning-card>
</template>

In this example, the @wire decorator is used to call the getUserDetails Apex method and store the result in the userDetails property. The connectedCallback method is then called when the component is loaded, and it calls the fetchUserDetails method to populate the userName and accountId properties.

Note that this approach requires the use of the @AuraEnabled annotation on the Apex method, which is only available in Lightning Web Components. If you are using a different framework, you may need to use a different approach to fetch the user’s data.

Q&A

Am I able to utilize wire on a Lightning Home Page?

Yes, you can use wire on a Lightning Home Page.

Where can I get the User’s data such as name, account id, and related records?

You can retrieve the User’s information with an SOQL query in the Apex class.

How do I access the User’s Account and related Location records?

Utilize a nested SOQL query to fetch the Account and its related Location records.

Video Explanation:

The following video, titled "How to get the ID of currently logged in user in Wix Velo - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

In this tutorial I will be showing you how to get the user ID of the currently logged in user in Wix using Velo.