0

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
0

2 Answers 2

0

All API calls in Salesforce only return unique records; as far as I'm aware, I don't know of any databases would return nonunique records just from the filters. Also, yes, you can only query records in the org your are currently in with this method. It would require an API call-out to reach other orgs.

5
  • @sfdxfox I should have said distinct uids, not unique. I edited the post for clarity. Please see my above question. Thanks!
    – Kristina
    Oct 19, 2022 at 21:12
  • @Kristina you have the same id twice in your recordIds (005xx000001X7sH). As @sfdcfox mentioned, I would expect that to return 1 record since its the same id Oct 19, 2022 at 21:18
  • ah I see. Thanks @usernameabc
    – Kristina
    Oct 19, 2022 at 21:45
  • @Kristina distinct/unique has the same semantics as far as we're concerned here.
    – sfdcfox
    Oct 19, 2022 at 23:53
  • @sfdcfox Ok, thanks for the info.
    – Kristina
    Oct 20, 2022 at 17:33
0

please find the workaround for retrieving many records, even of different objects in the following article: https://dev.to/tdrnk/lwc-getrecords-wire-adapter-how-to-retrieve-records-dynamically-apd.

Try using the parameterObject that will include all the recordIds to retrieve. It may be filled in the ConnectedCallback:

            this.recordIds.forEach(record=> {
                this.parameterObject.push({
                    recordIds: [record.Id],
                    fields: FIELDS
                });
            });

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .