Chain Wire methods in LWC

by Rijwan Mohmmed
0 comment

Hello friends, today we are going to discuss Chain Wire methods in LWC(Lightning Web Component). Many times we face issues when there are multiple Wire methods but they start randomly. So We have a solution for this type of issue.

Also, check this: Upload Image by HTTP Callout in LWC

The value set by one wire method may be dependent on the value set by another wire method. Using chaining, we can arrange the wire methods accordingly. If we pass parameters of the Wire method, that parameters values are set in another Wire method then they run orderly. So first parameters values set in Wire method so it calls first, then another method will call which have parameters.


Key Highlights :

  1. We can track the Wire method.
  2. We can handle the execution of wire methods

Code :

Here I create 2 Wire methods in which one method depends on another Wire method.

AccountDataController.cls :

public class AccountDataController {
    @AuraEnabled (cacheable=true)
    public static List<Account> fetchAccounts(){
        return [SELECT Id, Name, BillingPostalCode, BillingCountry 
                FROM Account LIMIT 10];       

    @AuraEnabled (cacheable=true)
    public static List<Opportunity> fetchOpportunity(String accountId){
        return [SELECT Id, Name, Account.Name, AccountId, StageName, Amount, CloseDate 
                FROM Opportunity WHERE AccountId =:accountId LIMIT 10];       

chainWireMethodLWC.HTML :


    <div class="slds-tabs_card">
		<div class="slds-page-header">
			<div class="slds-page-header__row">
				<div class="slds-page-header__col-title">
					<div class="slds-media">
						<div class="slds-media__figure">
							<span class="slds-icon_container slds-icon-standard-opportunity">
								 <lightning-icon icon-name="standard:recipe" alternative-text="recipe" title="recipe"></lightning-icon>
						<div class="slds-media__body">
							<div class="slds-page-header__name">
								<div class="slds-page-header__name-title">
										<span>Chain Wire methods in LWC Salesforce </span>
										<span class="slds-page-header__title slds-truncate" title="Recently Viewed">TechDicer</span>
	</div> <br/>

    <lightning-card title="Chain Wire methods in LWC Salesforce" icon-name="standard:opportunity">
        <div class="slds-var-p-around_small"> 
            <b>Retrive Account Id : {accountId}</b>
        <template if:true={opportunityList}>
            <lightning-datatable key-field="Id" 

chainWireMethodLWC.JS :

import { LightningElement, wire, track } from 'lwc';
import fetchAccounts from "@salesforce/apex/AccountDataController.fetchAccounts";
import fetchOpportunity from "@salesforce/apex/AccountDataController.fetchOpportunity";
const columns = [
        label: 'Name',
        fieldName: 'Name',
        type: 'text',
    }, {
        label: 'Stage Name',
        fieldName: 'StageName',
        type: 'text'
    }, {
        label: 'Amount',
        fieldName: 'Amount',
        type: 'currency'

export default class ChainWireMethodLWC extends LightningElement {
    @track accountId;
    @track opportunityList;
    columns = columns;

    @wire(fetchAccounts, {})  
    wiredAccount( { error, data } ) {
        if(data) {
            this.accountId = data[0].Id;
        } else if (error) {

    @wire(fetchOpportunity, {accountId : '$accountId'})  
    wiredOpportunity( { error, data } ) {
        if(data) {
            this.opportunityList = data;
        } else if (error) {

chainWireMethodLWC.js-meta.xml :

<?xml version="1.0"?>
<LightningComponentBundle xmlns="">

Output :


Reference :

  1. Wire Property in LWC

You may also like

Leave a Comment