lwc wire service
Lightning Web Components

How to use LWC wire service in salesforce

The Salesforce LWC wire service is a powerful feature of Lightning Web Components (LWC) that allows you to handle data without writing Apex code. The wire service allows you to fetch data from an Apex class and use it in your LWC component. Here’s an example of how to use the wire service in an LWC component:

In the above example, the getAccounts method is an Apex class that returns a list of accounts. The @wire decorator is used to call this method and the returned data is stored in the accounts property.

public with sharing class AccountController {

    @AuraEnabled(cacheable = true)
    public static List<Account> getAccounts() {
        return [SELECT Id, Name FROM Account];

The Apex class is decorated with @AuraEnabled annotation which makes it accessible to LWC. The cacheable attribute is set to true so that the data is cached on the client-side and not refetched on every request.

In the HTML template of the component, we can now use the data from the accounts property to display a list of accounts:

    <template if:true={accounts.data}>
        <template for:each={accounts.data} for:item="account">
            <p key={account.Id}>{account.Name}</p>

This is a basic example of how to use the wire service in an LWC component to fetch data from an Apex class. You can also pass parameters to the Apex method and handle errors that occur when calling the method.

Leave A Comment