1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Early on when I was having issues, I asked ChatGPT if it were possible, and it said, "As of my last update in September 2021, Lightning Web Components (LWC) with wire adapters are not supported on Lightning Home Pages. Wire adapters are designed to work with specific contexts, such as record pages and standalone Lightning apps, but they do not work on the Lightning Home Page." That said, I'll be happy if it's completely wrong. I'm fairly new to all of this. I'll test that solution and get back to you.
You do realise language models can "invent" fictional references, argue with you that there's a science journal article with such and such title, authors... it probably got creative with answers to salesforce.stackexchange.com/q/378694/799
Of course, I wasn't using it as a reliable reference. Rather, I was hoping to reduce the research time by asking it for answers to parts of it, and one of the answers is that it didn't work. It turns out, Wire works perfectly fine after all. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.