0

I'm struggling to find a way to set my accountID value to the returned value of my get parentAccountName() method.

The method works and returns the value successfully, but when trying to assign it to accountId (this is then passed into a SOQL query inside of an Apex class) it doesn't work.

Tried looking around for countless hours now and can't get to the bottom of why so I'm wondering if someone is able to help me with this.

import ACCOUNT_NAME from "@salesforce/schema/Case.Account.Id";

    @wire(getRecord, {
    recordId: '$recordId',
    fields: [Subject, ACCOUNT_NAME]
})
account;

get parentAccountName() {
    return getFieldValue(this.account.data, ACCOUNT_NAME);
}

doInit() {
    init({
            accountId: '$parentAccountName'
        })
        .then(result => {
            this.listOfRelatedOpenComplaints = [];
            result.forEach((row) => {
                console.log(row);
                let rowData = {};
                rowData.Url = '/' + row.Id;
                rowData.Complaint_Reference__c = row.Complaint_Reference__c;
                rowData.Subject = row.Subject;
                rowData.Status = row.Status;
                rowData.CreatedDate = row.CreatedDate;
                this.listOfRelatedOpenComplaints.push(rowData);
            });
        })
        .catch(error => {
            console.log(error);
        })
}
2
  • Why do you have a doInit function in an LWC? That is an Aura component construct.
    – Phil W
    Oct 18 at 9:46
  • @PhilW It was taken from a guide to help build a data table Oct 18 at 10:02
1

Since your row data depends on the result of a wire, you need to ensure that the "initialization" is performed after that wire returns the required value.

Note, also, that doInit is not an LWC construct, but rather is Aura's equivalent to the LWC connectedCallback. However, you cannot use LWC's connectedCallback because this would be called before the wire has been invoked.

In addition, only wires use "dynamic, reactive" parameters referenced via the $name syntax. Imperative calls (such as the one you do to init) should just receive the required value. In this case I'm guessing you actually need the ID rather than name of the Account...

So, try changing the LWC to something like:

// Holds the wire data
account;

@wire(getRecord, {
    recordId: '$recordId',
    fields: [Subject, ACCOUNT_NAME]
})
receiveAccount({data, error}) {
    if (data) {
        this.account = data;
        doInit();
    }

    if (error) {
        ...; // React to an error in some appropriate way
    }
}

get parentAccountName() {
    if (this.account) {
        return getFieldValue(this.account, ACCOUNT_NAME);
    }

    return null;
}

doInit() {
    init({
            // Pass the required value. I'm guessing it should be the
            // account ID, which is actually already in recordId
            accountId: this.recordId
        })
        .then(result => {
            this.listOfRelatedOpenComplaints = [];
            result.forEach((row) => {
                console.log(row);
                let rowData = {};
                rowData.Url = '/' + row.Id;
                rowData.Complaint_Reference__c = row.Complaint_Reference__c;
                rowData.Subject = row.Subject;
                rowData.Status = row.Status;
                rowData.CreatedDate = row.CreatedDate;
                this.listOfRelatedOpenComplaints.push(rowData);
            });
        })
        .catch(error => {
            console.log(error);
        })
}
3
  • Hi Phil, Thanks so much for the reply already! I appreciate the help... I should have specified, the LWC is intended for the Case page, and the purpose of the LWC is to retrieve (and display in the datatable) a list of Complaint Cases (custom record type) related to the Account. So based off that, I'm guessing accountId: this.recordID' wouldn't retrieve the records I need as it's going off the current Case record Id, and not the Case's parent, account ID? Oct 18 at 11:26
  • If you have the correct account ID in this.account then you can use this.account.Id to access the account's ID.
    – Phil W
    Oct 18 at 11:28
  • Oh, and obviously the fetching of the account itself would need adjusting accordingly. I guess the this.account would actually be this.case and this.case.accountId would be the field you want to use to pass the account ID...?
    – Phil W
    Oct 18 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.