Concepts or coding lessons of Salesforce that you can implement easily

Get records from different Objects using Wire Method

How to use @Wire to get data for Batch of records ??

In LWC project, we usually have scenario where we have to retrieve different records from different object. For this we use Wire method but are you using 2 wire methods for this ?

Then you are doing multiple calls which decreases performance. 

LWC have 'getRecords' using this we can use wire adapter to get data for a batch of records at once.

As this process in Batch style, this reduces the number of network calls which indirectly increases LWC performance.

Syntax:

@wire(getRecords, { records: [ { recordIds: string[], fields: string[] } ] })


This will return 2 parameters
Results which is batch result
hasErrors which indicates if any result is failed.


Note : If you want to get data for a single record, use getRecord instead.


Here is the example you can use:

import { LightningElement, wire } from 'lwc';
import { getRecords } from 'lightning/uiRecordApi';
import CONTACT_EMAIL_FIELD from '@salesforce/schema/Contact.Email';
import USER_EMAIL_FIELD from '@salesforce/schema/User.Email';
import USER_NAME_FIELD from '@salesforce/schema/User.Name';

export default class RecordPickerDemo extends LightningElement {
    @wire(getRecords, { 
        records: [
            {
                recordIds : ['0050XXXXXX8QSMo'],
                fields: [USER_EMAIL_FIELD],
                optionalFields: [USER_NAME_FIELD],
            },
            {
                recordIds : ['0032XXXXXXnuFFf'],
                fields: [CONTACT_EMAIL_FIELD],
            },
        ],
    })
    wiredRecords;
}