Retrieve Data using @wire Decorator in LWC

Learn how to use Wire Adapters in Lightning Web Components to retrieve Salesforce data declaratively — without writing Apex code.

1. Introduction

Wire adapters are used in Lightning Web Components (LWC) to connect components to Salesforce and retrieve data. They provide data and metadata, such as individual records, lists of records, and object and layout schema.

One of the main benefits of using wire adapters is that you typically don't need to write Apex code for many common data operations, making the development process faster and easier.

In this post, we will explore the advantages and detailed information about these wire adapters:
1. getRecord
2. getRecords
3. getObjectInfo
4. getPicklistValues
5. getRelatedRecords
6. getListInfo

2. getRecord

This wire adapter retrieves data for a single Salesforce record. It automatically fetches the specified fields for the record with the provided ID.

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import EMAIL_FIELD from '@salesforce/schema/Contact.Email';

export default class ContactRecord extends LightningElement {
    @api recordId;

    @wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD, EMAIL_FIELD] })
    contact;

    get name() {
        return this.contact.data ? this.contact.data.fields.Name.value : '';
    }

    get email() {
        return this.contact.data ? this.contact.data.fields.Email.value : '';
    }
}
<template>
    <p>Name: {name}</p>
    <p>Email: {email}</p>
</template>

getRecord is imported from lightning/uiRecordApi.
• Fields are imported using @salesforce/schema — this ensures type safety and field references.
• The $recordId prefix means the wire will re-execute whenever recordId changes.

3. getObjectInfo

This wire adapter retrieves metadata for an object. It retrieves metadata information (such as field definitions) for the specified object.

import { LightningElement, wire } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJ from '@salesforce/schema/Account';

export default class AccountInfo extends LightningElement {
    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJ })
    accountInfo;

    get apiName() {
        return this.accountInfo.data ? this.accountInfo.data.apiName : '';
    }

    get label() {
        return this.accountInfo.data ? this.accountInfo.data.label : '';
    }
}
<template>
    <p>Object API Name: {apiName}</p>
    <p>Object Label: {label}</p>
</template>

getObjectInfo is imported from lightning/uiObjectInfoApi.
• Returns metadata like apiName, label, fields, recordTypeInfos etc.
• Useful for building dynamic forms that adapt based on object metadata.

4. getPicklistValues

This wire adapter fetches picklist values for a specific field on an object, allowing you to dynamically populate dropdowns and other selection elements.

import { LightningElement, wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';

export default class IndustryPicklist extends LightningElement {
    @wire(getPicklistValues, { fieldApiName: INDUSTRY_FIELD })
    picklistValues;

    get options() {
        return this.picklistValues.data ? this.picklistValues.data.values : [];
    }
}
<template>
    <lightning-combobox options={options} placeholder="Select an Industry">
    </lightning-combobox>
</template>

getPicklistValues is imported from lightning/uiObjectInfoApi.
• The fieldApiName parameter specifies which picklist field to fetch values for.
picklistValues.data.values returns an array of {label, value} objects — perfect for lightning-combobox.

5. getRelatedRecords

getRelatedRecords retrieves records related to a parent record using a relationship field. This simplifies accessing related data without complex queries.

import { LightningElement, api, wire } from 'lwc';
import { getRelatedRecords } from 'lightning/uiRelatedListApi';
import ACCOUNT_ID from '@salesforce/schema/Contact.AccountId';

export default class RelatedContacts extends LightningElement {
    @api parentAccountId;

    @wire(getRelatedRecords, {
        parentRecordId: '$parentAccountId',
        relationshipField: ACCOUNT_ID
    })
    relatedContacts;

    get contactNames() {
        return this.relatedContacts.data ?
            this.relatedContacts.data.records.map(record =>
                record.fields.Name.value).join(', ') : '';
    }
}
<template>
    <p>Related Contacts: {contactNames}</p>
</template>

getRelatedRecords is imported from lightning/uiRelatedListApi.
parentRecordId — the ID of the parent record.
relationshipField — the field that defines the relationship between the objects.

6. getListInfo

This wire adapter retrieves metadata for a list view on an object, allowing you to create dynamic lists and views based on the user's preferences and access levels.

import { LightningElement, api, wire } from 'lwc';
import { getListInfo } from 'lightning/uiListApi';
import ACCOUNT_OBJ from '@salesforce/schema/Account';

export default class AccountListInfo extends LightningElement {
    @api listViewApiName = 'AllAccounts';

    @wire(getListInfo, { objectApiName: ACCOUNT_OBJ, listViewApiName: '$listViewApiName' })
    listInfo;

    get listLabel() {
        return this.listInfo.data ? this.listInfo.data.label : '';
    }

    get listColumns() {
        return this.listInfo.data ? this.listInfo.data.columns.map(col => col.label) : [];
    }
}
<template>
    <p>List View Label: {listLabel}</p>
    <p>List Columns: {listColumns.join(', ')}</p>
</template>

getListInfo is imported from lightning/uiListApi.
• Returns metadata about the list view including label, columns, and filter logic.
• Useful for building custom list views that mirror Salesforce's native list views.

7. Wire Adapter Quick Reference

getRecordlightning/uiRecordApi — Fetch a single record's field values.
getObjectInfolightning/uiObjectInfoApi — Fetch object metadata (field definitions, labels).
getPicklistValueslightning/uiObjectInfoApi — Fetch picklist values for a field.
getRelatedRecordslightning/uiRelatedListApi — Fetch records related to a parent record.
getListInfolightning/uiListApi — Fetch list view metadata for an object.

📝 Key Rules for @wire:
• Always use $propertyName (with $ prefix) to make a wire parameter reactive.
• Wire result always has .data and .error — always check if (data) before accessing values.
• Wire adapters use caching — they do not re-call the server if data is already cached.
• Wire is read-only — use imperative Apex for data mutations (create, update, delete).

Popular posts from this blog

Handling Errors in LWC

Hello World using LWC

Lightning Card in LWC