Introducing getRecords: A Wire Adapter for Retrieving Batch Records in LWC!


With the getRecords wire adapter, you can now streamline your data fetching process by retrieving data for a batch of records all at once. 

This powerful adapter allows you to request multiple objects or different record types, simplifying your development workflow on the Lightning Web Component (LWC) framework.

Syntax:

import { LightningElement, wire } from 'lwc';
import { getRecords } from 'lightning/uiRecordApi';

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

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


Parameters:

records - An array of record data, which can be across multiple objects or record types.

recordIds - (At least one required) The ID of records to fetch from supported objects.

fields - An array of fields to return. If the context user doesn’t have access to a field, an error is returned. 

Use the format ObjectApiName.FieldName or ObjectApiName.JunctionIdListName.

optionalFields - (Optional) An array of optional field names. 

If a field is accessible to the context user, it’s included in the response. 

Use the format ObjectApiName.FieldName or ObjectApiName.JunctionIdListName.

Key Features:

Utilizes the User Interface API Resource GET /ui-api/records/batch/{recordIds}

Supports fetching record data across multiple objects or record types.

Fetches records based on provided recordIds.

Specify the fields to return using the fields parameter.

Handles inaccessible fields gracefully with the optionalFields parameter.

Provides results in a results object alongside an indicator (hasErrors) for any HTTP status code errors.

Usage Tips:

For single record retrieval, consider using the getRecord wire adapter instead.

Avoid using polymorphic fields in the fields parameter as they aren't supported and may cause errors.

Include optional fields using the optionalFields parameter when unsure about accessibility.

Returns:

results - Batch Results.

hasErrors - true if at least one of the results in the result set is an HTTP status code in the 400 or 500 range, or false otherwise.

Example Code:

import { LightningElement, wire } from "lwc";
import { getRecords } from "lightning/uiRecordApi";
import NAME_FIELD from "@salesforce/schema/User.Name";
import EMAIL_FIELD from "@salesforce/schema/User.Email";

export default class GetRecordsExample extends LightningElement {
  @wire(getRecords, {
    records: [
      {
        recordIds: ["005XXXXXXXXXXXXXXX", "005XXXXXXXXXXXXXXX"],
        fields: [NAME_FIELD],
        optionalFields: [EMAIL_FIELD],
      },
    ],
  })
  wiredRecords;
}

Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

label Labels

Comments 0