I have a project with the following requirements:
- Must work on a Salesforce Community Page (currently testing in a Lightning Home Page).
- 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>