
Salesforce’s Lightning Web Components (LWC) have revolutionized the way developers build user interfaces on the Salesforce platform. One of the key features that contribute to the success of LWC is the Wire Service (Wire Adapters), a mechanism that allows components to communicate with the Salesforce platform by wiring properties or calling Apex methods. This is achieved through wire adapters, which play a crucial role in fetching and manipulating data efficiently. In this blog post, we’ll delve into the 10 most commonly used LWC wire adapters, exploring their functionalities and use cases.
𝟙. @wire(getRecord)
The @wire(getRecord) adapter is used to retrieve a single record from Salesforce, identified by its record ID. This wire adapter is immensely valuable when you need to display or manipulate data from a specific record. It simplifies data retrieval and ensures that your component always has the most up-to-date information.
import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class RecordDetails extends LightningElement {
@api recordId;
/* @wire(getRecord, { recordId: string, fields: string|string[], optionalFields?: string|string[] })
propertyOrFunction
@wire(getRecord, { recordId: string, layoutTypes: string|string[],
modes?: string|string[], optionalFields?: string|string[] })
propertyOrFunction
*/
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name', 'Account.Type'] })
account;
}
𝟚. @wire(getRecords)
Use this wire adapter to get data for a batch of records at once. You can request multiple objects or different record types.
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
𝟛. @wire(getFieldValue)
Gets a field’s value from a record. Spanning fields are supported.
The field’s value is returned in its raw data form. In some cases, the raw data form differs from the display value that’s returned by getFieldDisplayValue(record, field).
import { LightningElement, api, wire } from "lwc";
import { getRecord, getFieldValue } from "lightning/uiRecordApi";
import REVENUE_FIELD from "@salesforce/schema/Account.AnnualRevenue";
import CREATED_FIELD from "@salesforce/schema/Account.CreatedDate";
import EXP_FIELD from "@salesforce/schema/Account.SLAExpirationDate__c";
const fields = [REVENUE_FIELD, CREATED_FIELD, EXP_FIELD];
export default class WireGetValue extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: "$recordId", fields })
account;
get revenue() {
return getFieldValue(this.account.data, REVENUE_FIELD);
}
get created() {
return getFieldValue(this.account.data, CREATED_FIELD);
}
get expiration() {
return getFieldValue(this.account.data, EXP_FIELD);
}
}
𝟜. @wire(getListInfoByName)
Use this wire adapter to get the metadata for a list view.
import { LightningElement, wire } from "lwc";
import { getListInfoByName } from "lightning/uiListsApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";
export default class Example extends LightningElement {
@wire(getListInfoByName, { objectApiName: ACCOUNT_OBJECT, listViewApiName: "AllAccounts" })
propertyOrFunction;
}
getListInfosByName : Use this wire adapter to get the metadata for a batch of list views.
import { getListInfosByName } from "lightning/uiListsApi";
import ACCOUNT_OBJECT from "@salesforce/schema/Account";
export default class KeywordResults extends LightningElement {
@wire(getListInfosByName, {
names: ["${ACCOUNT_OBJECT.objectApiName}.AllAccounts"],
})
getListInfosByNameWire({ data }) {
if (data && data.results) {
this.listInfos = data.results.map(({ result }) => result);
}
}
}
𝟝. @wire(getPicklistValues):
The @wire(getPicklistValues) adapter simplifies the retrieval of picklist values for a specific field on a record. It’s a powerful tool when building dynamic forms or components that require information about available picklist options.
import { LightningElement, wire, api } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
export default class PicklistOptions extends LightningElement {
@api objectApiName;
@api fieldApiName;
@wire(getPicklistValues, { recordTypeId: '$recordTypeId', fieldApiName: '$fieldApiName' })
picklistValues;
}
𝟞. @wire(getObjectInfo):
The @wire(getObjectInfo) adapter provides information about a specific Salesforce object, such as its fields and metadata. This is useful when you need to dynamically generate forms or validate input based on the properties of an object.
import { LightningElement, wire, api } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
export default class ObjectDetails extends LightningElement {
@api objectApiName;
@wire(getObjectInfo, { objectApiName: '$objectApiName' })
objectInfo;
}
𝟟. @wire(notifyRecordUpdateAvailable(recordIds))
The getRecordNotifyChange(recordIds) retrieves updates for the designated record IDs and updates the Lightning Data Service cache, ensuring that your wire adapters receive the most recent record information. However, it’s important to note that using getRecordNotifyChange(recordIds) with multiple adapters returning data of the same type is not recommended.
import { LightningElement, wire } from 'lwc';
import { getRecord, notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import apexUpdateRecord from '@salesforce/apex/Controller.apexUpdateRecord';
export default class Example extends LightningElement {
@api recordId;
// Wire a record
@wire(getRecord, { recordId: '$recordId', fields: ... })
record;
async handler() {
// Do something before the record is updated
showSpinner();
// Update the record via Apex
await apexUpdateRecord(this.recordId);
// Notify LDS that you've changed the record outside its mechanisms
// Await the Promise object returned by notifyRecordUpdateAvailable()
await notifyRecordUpdateAvailable([{recordId: this.recordId}]);
hideSpinner();
}
}
𝟠. @wire(getObjectInfos)
The @wire(getObjectInfos) adapter is similar to @wire(getObjectInfo) but allows you to fetch information for multiple objects in a single wire request. This is advantageous when dealing with components that require data from multiple objects.
import { LightningElement, wire, api } from 'lwc';
import { getObjectInfos } from 'lightning/uiObjectInfoApi';
export default class MultiObjectDetails extends LightningElement {
@api objectApiNames;
@wire(getObjectInfos, { objectApiNames: '$objectApiNames' })
objectInfos;
}
𝟡. @wire(getNavItems)
The @wire(getNavItems) (Beta) adapter allows you to retrieve navigation items, such as the app’s navigation menu. This is beneficial when you want to create a customized navigation experience within your Lightning components.
import { LightningElement, wire } from 'lwc';
import { getNavItems } from 'lightning/uiAppsApi';
import FORM_FACTOR from '@salesforce/client/formFactor';
export default class Example extends LightningElement {
@api tabs = ['standard-Account', 'standard-CollaborationGroup'];
@wire(getNavItems, {
formFactor: FORM_FACTOR,
navItemNames: '$tabs',
pageSize: 30,
})
propertyOrFunction;
𝟙𝟘. wire(getRelatedListRecords)
Use this wire adapter to get RelatedList records. Related lists display details and links to records that are associated with a specific record. For example, an account can have related lists for contacts, cases, notes, or even files
import { LightningElement, wire } from 'lwc';
import { getRelatedListRecords } from 'lightning/uiRelatedListApi';
export default class LdsGetRelatedListRecords extends LightningElement {
@wire(getRelatedListRecords, {
parentRecordId: '001RM000003UNu6YAG',
relatedListId: 'Contacts',
fields: ['Contact.Name','Contact.Id'],
sortBy: ['Contact.Name']
})
}
getRelatedListRecordBatch Use this wire adapter to get records for a batch of RelatedLists.
import { LightningElement, wire } from 'lwc';
import { getRelatedListRecordsBatch } from 'lightning/uiRelatedListApi';
export default class LdsGetRelatedListRecordsBatch extends LightningElement {
@wire(getRelatedListRecordsBatch, {
parentRecordId: '001RM000003UNu6YAG',
relatedListParameters: [
{
relatedListId: 'Contacts',
fields: ['Contact.Name','Contact.Id'],
sortBy: ['Contact.Name']
},
{
relatedListId: 'Opportunities',
fields: ['Opportunity.Name', 'Opportunity.Amount'],
sortBy: ['Opportunity.Amount'],
where: "{ and: [{ Name: { like: \"ACME%\" }}] }"
}
]
})
}
Conclusion
Lightning Web Components wire adapters are powerful tools that simplify data retrieval and manipulation in Salesforce. By understanding the functionalities of these commonly used wire adapters, developers can build more efficient, dynamic, and responsive user interfaces on the Salesforce.
References
https://developer.salesforce.com/docs/platform/lwc/guide/reference-ui-api.html
About the blog
SFDCLessons is a blog where you can find various Salesforce tutorials and tips that we have written to help beginners and experienced developers alike. we also share my experience and knowledge on Salesforce best practices, troubleshooting, and optimization. Don’t forget to follow us on:
Newsletter
Subscribe to our email newsletter to be notified when a new post is published.
