Fetch multiple objects info in LWC
In LWC one of the major challenges is we need to import everything before we use it. At times it can be very frustrating as we need to import objects, fields, LDS methods and, etc.
That too, when you need to import info related to metadata describing the fields, child relationships, record type, and theme for each object it becomes very tedious. Hence LWC comes to our resuce here by providing us an out of box helper named import getObjectInfos
Let's look at an example!
import { LightningElement, wire } from 'lwc';
//import the named import
import { getObjectInfos } from 'lightning/uiObjectInfoApi';
//import references to multiple objects
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
export default class GetObjectInfosExample extends LightningElement {
//wire the named import to a propery or function
@wire(getObjectInfos, { objectApiNames: [ ACCOUNT_OBJECT, OPPORTUNITY_OBJECT ] })
objectsInfo({error, data}){
if(data){
console.log(data);
}else{
console.log(error);
}
}
}
The response that we get back is going to have info about the objects in the order that we have expected.
In this above example am trying to wire the output of a method to a function and logging it using the notation data
whereas if you try to wire it to a property then you need to use the notation propertyName.data
to fetch the requested info.
Hope this was helpful!