Introduction
The @wire decorator in Salesforce Lightning Web Components (LWC) is a powerful tool for accessing data from Salesforce database, such as Apex methods or Salesforce’s built-in wire adapters.
It simplifies the process of fetching, caching, and updating data within your components.
In this blog post, we will understand the @wire decorator, its use cases, syntax, and practical examples, helping you leverage its potential in Salesforce development.
If you’re unfamiliar with Decorators, I recommend checking out the posts below to learn about the different types of LWC decorators in Salesforce.
What is the @wire decorator?
- Wire decorator is used to read data from Salesforce database by connecting your LWC component to Salesforce data source.
- Wire decorator is reactive in nature, which means – whenever the data in Salesforce org changes, then automatically the component re-renders and the updated data is displayed on the LWC component.
The @wire decorator can be applied to:
- Properties: Automatically receives and holds data for use in the component.
- Functions: Offers more flexibility to process or manipulate the fetched data.
Common Use Cases for @wire
- Fetching Salesforce data using wire adapters (e.g., getRecord, getListUi).
- Invoking Apex methods for custom business logic.
- Leveraging Lightning Data Service (LDS) for declarative access to Salesforce records.
- Optimizing data caching for better performance and efficiency.
Syntax
import { LightningElement, wire } from ‘lwc’;
import from ‘@salesforce/apex/’;
@wire(methodName, {methodParams})
- methodName – It identifies the apex method.
- ApexClassName – It is the apex class name
- apexMethod – Method of apex class
1. Using @wire with a Property
@wire(wireAdapter, config)
propertyName;
2. Using @wire with a Function
@wire(wireAdapter, config)
wiredFunction({ error, data }) {
// Handle data or error
}
Example 1: Using @wire with getRecord to Fetch a Record
This example demonstrates how to use @wire to fetch a Salesforce record.
JavaScript: exampleComponent.js
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
// Specify the fields to fetch
const FIELDS = ['Account.Name', 'Account.Industry', 'Account.Phone'];
export default class ExampleComponent extends LightningElement {
@api recordId; // Record ID passed from parent or environment
@wire(getRecord, { recordId: '$recordId', fields: FIELDS })
account;
get accountName() {
return this.account.data ? this.account.data.fields.Name.value : 'N/A';
}
get accountIndustry() {
return this.account.data ? this.account.data.fields.Industry.value : 'N/A';
}
get accountPhone() {
return this.account.data ? this.account.data.fields.Phone.value : 'N/A';
}
}
HTML: exampleComponent.html
Account Name: {accountName}
Industry: {accountIndustry}
Phone: {accountPhone}
Example 2: Using @wire with Apex
This example shows how to call an Apex method using @wire.
Apex Class: AccountController.cls
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List getRecentAccounts() {
return [SELECT Id, Name, Industry FROM Account ORDER BY CreatedDate DESC LIMIT 5];
}
}
JavaScript: exampleComponent.js
import { LightningElement, wire } from 'lwc';
import getRecentAccounts from '@salesforce/apex/AccountController.getRecentAccounts';
export default class ExampleComponent extends LightningElement {
accounts;
error;
@wire(getRecentAccounts)
wiredAccounts({ error, data }) {
if (data) {
this.accounts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.accounts = undefined;
}
}
}
HTML: exampleComponent.html
- {account.Name} - {account.Industry}
Error: {error.body.message}
Key Points About @wire
- Declarative Data Fetching: Simplifies data fetching by binding directly to a wire adapter or Apex method.
- Automatic Caching: Automatically caches data for improved performance. Use the @AuraEnabled(cacheable=true) annotation for Apex methods to take advantage of this feature.
- Error Handling: Provides built-in error handling through { error, data } structure.
- Reactivity: Automatically re-fetches data if the dynamic parameters in the wire configuration change.
- Read-Only Data: Data retrieved via @wire is read-only; to modify it, create a mutable copy.
When to Use @wire
- Fetching frequently accessed Salesforce records or lists.
- Using built-in wire adapters like getRecord, getObjectInfo, or getPicklistValues.
- Calling Apex methods for custom logic while leveraging caching.
- Reacting to changes in parameters dynamically.
Limitations of @wire
- Cannot use @wire for imperative calls; use the standard Apex invocation pattern (@AuraEnabled) instead.
- Data is read-only and cannot be directly mutated.
Checkout below posts to explore other 2 LWC Decorators in Salesforce
Conclusion
The @wire decorator is an important tool for Salesforce LWC developers, enabling seamless integration with Salesforce data and services.
Its declarative approach simplifies data handling while enhancing performance through caching and reactivity.
For more expert content on Salesforce development, visit Writtee.com. Let us know in the comments how you’ve used @wire in your projects.
Happy coding! 🚀