getRecords
Use this wire adapter to get data for a batch of records at once. You can request multiple objects or different record types.
The getRecords
wire adapter uses this User Interface API resource, but doesn’t support all its parameters.
Parameter Name | Type | Description | Required? |
---|---|---|---|
records | Object | 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 . 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’sdata
property orerror
property. - If a function is decorated with
@wire
, the results are returned in an object with adata
property and anerror
property.
results
—Batch Resultserror
-FetchResponse
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.
This example loads several records using the record IDs. Replace the recordIds
values with your own.
Alternatively, you can request for records across multiple objects.
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.
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.
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.
The lwc-recipes repo has several examples that demonstrate getRecords
usage. Look for components that start with wireGetRecords
, such as wireGetRecordsDifferentTypes
component.
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.
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 a non-200 error code or 400 error code, the
results.result
object contains theerrorCode
andmessage
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.
To display errors, you can use toasts provided by the lightning/platformShowToastEvent
module with a Promise that includes then()
and catch()
blocks.