An array of record data, which can be across multiple objects or record types.
records has several properties.
records Property
Type
Description
Required?
recordIds
String[]
(At least one required) The ID of records to fetch from supported objects.
fields
String[]
An array of fields to return. If the context user doesn’t have access to a field, an error is returned. If you’re not sure whether the context user has access to a field and you don’t want the request to fail if they don’t, use the optionalFields parameter. Specify field names in the format ObjectApiName.FieldName or ObjectApiName.JunctionIdListName. Polymorphic fields aren’t supported. Including a polymorphic field in fields can result in an invalid field error.
optionalFields
String[]
An array of optional field names. If a field is accessible to the context user, it’s included in the response. If a field isn’t accessible to the context user, it isn’t included in the response, but it doesn’t cause an error. Specify field names in the format ObjectApiName.FieldName or ObjectApiName.JunctionIdListName.
Read the data that’s returned by the wire adapter using a property or function.
propertyOrFunction—A private property or function that receives the stream of data from the wire service.
If a property is decorated with @wire, the results are returned to the property’s data property or error property.
If a function is decorated with @wire, the results are returned in an object with a data property and an error property.
To get data for a single record, use getRecord instead.
To filter by criteria and work with dynamic record IDs easily, consider using the GraphQL wire adapter instead.
Tip
This example loads several records using the record IDs. Replace the recordIds values with your own.
1import{LightningElement, wire}from "lwc";2import{getRecords}from "lightning/uiRecordApi";3import NAME_FIELD from "@salesforce/schema/User.Name";4import EMAIL_FIELD from "@salesforce/schema/User.Email";56export default class GetRecordsExample extends LightningElement{7 @wire(getRecords, {8 records:[9{10 recordIds:["005XXXXXXXXXXXXXXX", "005XXXXXXXXXXXXXXX"],11 fields:[NAME_FIELD],12 optionalFields:[EMAIL_FIELD],13},14],15})16 wiredRecords;17}
Alternatively, you can request for records across multiple objects.
1import{LightningElement, wire}from "lwc";2import{getRecords}from "lightning/uiRecordApi";3import USER_NAME_FIELD from "@salesforce/schema/User.Name";4import USER_EMAIL_FIELD from "@salesforce/schema/User.Email";5import ACCOUNT_NAME_FIELD from "@salesforce/schema/Account.Name";67export default class GetRecordsExample extends LightningElement{8 @wire(getRecords, {9 records:[10{11 recordIds:["005XXXXXXXXXXXXXXX"],12 fields:[USER_NAME_FIELD],13 optionalFields:[USER_EMAIL_FIELD],14},15{16 recordIds:["001XXXXXXXXXXXXXXX"],17 fields:[ACCOUNT_NAME_FIELD],18},19],20})21 wiredRecords;22}
To work with dynamic record IDs, consider using the GraphQL wire adapter instead.
If you use the getRecords wire adapter with dynamic record IDs, we recommend that you create a parameterObject array that you can push your record parameters to as a single property. Using an array enables you to define record IDs and fields for multiple objects.
For example, you can retrieve contact IDs by account via an Apex controller and then pass them to the parameterObject array. You can also pass in your record parameters from a parent component to a child component that initializes the array.
When you call the wire adapter, the User Interface API composes a SOQL query for the request. This SOQL query has a limit of 100k characters. If you expect to exceed this limit, we recommend splitting the query into multiple queries and running them in batches.
Note
This example shows how you can retrieve contact and user records on an account. It assumes that you retrieve the contacts on an account using an Apex controller.
1public with sharing class ContactsController{2 @AuraEnabled(cacheable=true)3 public static List<Contact>getContactsByAccount(Id accountId){4 return[5 SELECT Id6 FROM Contact7 WHERE AccountId = :accountId8];9}10}
In your JavaScript, set up the parameterObject array to define record IDs and fields for the contact and user records. Then, pass in the $parameterObject dynamic variable to the getRecords wire adapter.
When you use this component on an account record page, it renders a list of contact names that are associated on the account and the username of the account owner.
getRecords returns errors in the error property. For example, you get a 400 Bad Request error if you pass in an invalid field name to the fields or optionalFields array.
1{2 "status": 400,3 "body": {4 "message": "Expected '.' in all qualified names: badFieldName is invalid",5 "statusCode": 400,6 "errorCode": "ILLEGAL_QUERY_PARAMETER_VALUE",7 "id": "1369078084"8},9 "headers": {},10 "ok": false,11 "statusText": "Bad Request",12 "errorType": "fetchResponse"13}
If the response status returns a 200 success code but a subrequest returns a statusCode of 400 or another non-200 error code, the network response returns hasErrors:true, but this property isn’t returned as part of data.
To identify errors in a subrequest, check the data.results object.
If the subrequest returns a 200 success code, the results.result object contains record data with the requested fields.
If the subrequest returns a non-200 error code or 400 error code, the results.result object contains the errorCode and message properties.
For example, you get an overall 200 success code if you request two records - a record with valid fields and a record with an invalid ID. Although the subrequest for the first record is returned with a 200 success code, the subrequest for the second record is returned with a 400 error code.
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.