I am trying to get an array of usernames from a list of user ids using getRecords. Using the documentation from getRecords, I am able to get one record. I intentionally used the same userid in the recordIds field because I will be using the records in a lightning datatable and I will need to display many of the same usernames in the table, but now I wonder if this adapter only retrieves distinct records. My code is below with the comments explaining the errors and some of the attempts to get an array of the usernames. Assuming the adapter only retrieves distinct records, can I only query recordIds from the org I am currently on?
import { LightningElement, api, track, wire } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecords } from 'lightning/uiRecordApi';
const FIELDS = ['User.Username'];
export default class Search extends LightningElement
users;
name;
@wire(getRecords, {
records: [
{
recordIds: ['005xx000001X7sH', '005xx000001X7sH'],
fields: FIELDS
}
]
})
wiredRecord({ error, data }) {
if (error) {
let message = 'Unknown error';
if (Array.isArray(error.body)) {
message = error.body.map((e) => e.message).join(', ');
} else if (typeof error.body.message === 'string') {
message = error.body.message;
}
this.dispatchEvent(
new ShowToastEvent({
title: 'Error loading contacts',
message,
variant: 'error'
})
);
} else if (data) {
this.users = data;
// when I remove the index [0] or change it to [1], the code breaks and I get an error:
// [LWC component's @wire target property or method threw an error during value provisioning. Original error:
// [Cannot read properties of undefined (reading 'fields')]] or [Cannot read properties of undefined (reading 'result')]]
this.name = this.users.results[0].result.fields.Username.value;
}
}
html file-
<p> {name} </p> --> [email protected] renders in the browser