Search code examples
javascripthtmlsalesforceapexlwc

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


I have a project with the following requirements:

  1. Must work on a Salesforce Community Page (currently testing in a Lightning Home Page).
  2. Must display a list of fields from related records.
  • There is an Account_Id__c field on User that contains the Account Id.
  • There are Location_Data__c records that look up to Account.
  • I would need the Location Name and a URL field from each Location that looks up to that account.

I have tried a few different ways of doing this on a Lightning Home Page to test. The issue I keep running into is that I can't seem to find a way to get the User data in the first place. The solutions I've tried rely on Wire, but wire cannot be used on a Lightning Home Page (as far as I'm aware).

What am I missing?

Error: List has no rows for assignment to SObject Class.UserDetailsController.getUserDetails: line 4, column 1

The following code does not display any data:

Apex Class

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

Javascript

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

export default class UserDetailsCard extends LightningElement {
    @api recordId;
    userName;
    accountId;

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

    async fetchUserDetails() {
        try {
            // Fetch the user record using the apex method.
            const userRecord = await getUserDetails({ userRecordId: this.recordId });
            if (userRecord) {
                this.userName = userRecord.Name;
                this.accountId = userRecord.Account_Id__c;
            } else {
                console.error('User not found.');
            }
        } catch (error) {
            // Handle any errors here.
            console.error('Error fetching user details:', 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>

Result with no data


Solution

  • wire cannot be used on a Lightning Home Page

    Do you have any documentation that statest that? I don't recall having any problems with @wire on home pages, normal or community.

    If it's related to the user - don't pass any parameters to apex. You can grab all you need server-side.

    SELECT Id, Name, (SELECT Id, Name, URL__c from Locations__r)
    FROM Account
    WHERE Id IN (SELECT Account_Id__c FROM User WHERE Id = :UserInfo.getUserId())
    

    You'll need to make sure the relationship name, field names are OK, it'll work only for users that have this lookup populated but hey, easier. Hook it up to normal @wire.

    It gets bit more complex if you want to be fancy and reuse same component for "on home page do it user-driven but if dropped on account's page fetch location_data__c for this account". In that case you do need to pass the record id to apex