Sub topic 12
Related adapters: getRecordUi, getRecordCreateDefaults, and getRecords
This chapter assumes you can wire getRecord with $recordId, read the data and error result, and import fields from @salesforce/schema. The three adapters below live in the same lightning/uiRecordApi module. They use the same Lightning Data Service cache and the same wire pattern, but each returns a different shape of data.
Choosing the adapter
| Adapter | Use it when | Main parameters | Result |
|---|---|---|---|
| `getRecordUi` | You need record data plus layout and object metadata for one or more records | `recordIds`, `layoutTypes`, `modes`, `optionalFields` | `records`, `layouts`, `objectInfos` |
| `getRecordCreateDefaults` | You are building a custom create form and need default values and a layout | `objectApiName`, `recordTypeId`, `formFactor`, `optionalFields` | `record`, `layout`, `objectInfos` |
| `getRecords` | You need fields from several records in one request | `records`, an array of request objects | `results`, an array of status and result pairs |
getRecordUi
getRecordUi returns the record and the page layout that describes how to display it. Use it when your component must follow an admin's layout, for example to render the same sections and fields the standard page shows.
import { LightningElement, api, wire } from 'lwc';
import { getRecordUi } from 'lightning/uiRecordApi';
export default class LayoutProbe extends LightningElement {
@api recordId;
@wire(getRecordUi, {
recordIds: '$recordId',
layoutTypes: ['Full'],
modes: ['View']
})
ui;
}The response is large because it bundles metadata with data. If you only need field values, getRecord is lighter. Salesforce also lets getRecord accept layoutTypes and modes, so check the current documentation before choosing getRecordUi for new work.
getRecordCreateDefaults
A custom create form needs to know which defaults apply before any record exists. getRecordCreateDefaults returns a record with default field values, the layout, and object metadata for a given object and record type.
import { LightningElement, wire } from 'lwc';
import { getRecordCreateDefaults } from 'lightning/uiRecordApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
export default class AccountCreateDefaults extends LightningElement {
@wire(getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT })
defaults;
}Import the object from @salesforce/schema instead of typing 'Account', for the same reason you import fields. The adapter only supplies defaults. You still need createRecord or a form component to save the record.
getRecords
getRecords fetches fields from several records in one call. Each entry in records names the record IDs and the fields to read. The result is a results array. Each item has its own statusCode, so one inaccessible record does not fail the whole request.
import { LightningElement, api, wire } from 'lwc';
import { getRecords, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountNames extends LightningElement {
@api accountIds = [];
names = [];
errorMessage;
get requests() {
return this.accountIds.length
? [{ recordIds: this.accountIds, fields: [NAME_FIELD] }]
: undefined;
}
@wire(getRecords, { records: '$requests' })
wiredRecords({ data, error }) {
if (data) {
this.names = data.results
.filter((item) => item.statusCode === 200)
.map((item) => getFieldValue(item.result, NAME_FIELD));
this.errorMessage = undefined;
} else if (error) {
this.errorMessage = error.body && error.body.message;
this.names = [];
}
}
}The getter returns undefined while there are no IDs. A wire with an undefined reactive value does not run, so no empty request is sent.
Common errors
- Reading
data.fieldsfromgetRecords. The records sit underdata.results[n].result. - Skipping the
statusCodecheck. A result with a non-200 code has an error inresult, not a record. - Using
getRecordUiwhen only two field values are needed. The extra layout and metadata add payload and cache entries you never use. - Forgetting that
getRecordCreateDefaultscreates nothing. It only reads.
Exercise
A component must show the Name of five related accounts. Which adapter fits best, and how does a user without access to one account affect the result?
Show answer
Use getRecords with one request object listing the five IDs and the Name field. Each item in results carries its own statusCode. The inaccessible account returns a non-200 status with an error in its result, while the other four still return records, so filter on statusCode === 200 before reading values.