Lwc wire decorator won’t trigger when one of properties is set to undefined

lightning-web-componentslwc-wire-adapterwire

So I am calling apex via wire decorator, and I am passing in 3 inputs. The accountId and contactId are dependent on other wire service getRecord, when I receive the data from getRecord and populate one of the two variables the second wire service won't trigger.

@wire(getRecord, {recordId: '$userId', fields: [AccountId, ContactId, SUSE_Administrative_Privileges]})
    wireUser({error, data}) {
        if (data) {
            if (getFieldValue(data, SUSE_Administrative_Privileges).includes('Opportunity Administrator')) {
                this.accountId = getFieldValue(data, AccountId);
                console.log('accountId', this.accountId);
            } else {
                this.contactId = getFieldValue(data, ContactId);
                console.log('contactId', this.contactId);
            }
        } else if (error) {
            console.error(error);
        }
    }

    sobjects;

    @wire(getSObjects, {accountId: '$accountId', contactId: '$contactId', dashboardFilter: '$dashboardFilterValue'})
    wireSObjects({error, data}) { //wont trigger second time?? why...}

Best Answer

I did not see this anywhere documented, but apparently the wire service won't trigger if any of the input variables is undefined.

When I set the other variable as null in the first wire service, the second wire service triggers.

if (getFieldValue(data, SUSE_Administrative_Privileges).includes('Opportunity Administrator')) {
    this.accountId = getFieldValue(data, AccountId);
    this.contactId = null;
    console.log('accountId', this.accountId);
} else {
    this.accountId = null;
    this.contactId = getFieldValue(data, ContactId);
    console.log('contactId', this.contactId);
}