1

I have an Account lookup field which gets Account ID, and need to get its Account Name on field change. (This is a Home tab component using Opportunity Form)

HTML (lightning-record-edit-form; object-api-name = Opportunity)

<lightning-input-field field-name={oppAccountField} onchange={handleAccountChange} required></lightning-input-field>

JS

accountId = '';

@wire(getRecord, {
    recordId: '$accountId',
    fields: [ ACCOUNT_NAME_FIELD ]
})
thisAccount({ error, data }) {
    if (data) {
            console.log('got data');
            this.accountName = getFieldValue(data, ACCOUNT_NAME_FIELD);
    }
    if (error) {
        console.log('error');
        handleError(error);
    }
}

    handleAccountChange(e) {
            this.accountId = getFieldValue(e.detail.value, ACCOUNT_ID_FIELD);
            console.log('new acc Id: ' + this.accountId); //shows correct id
    }

Tis updates the this.accountId with accurate id, but does not @wire getRecord. Also, the getRecord works perfectly if I just hardcode random Id inside handleAccountChange(), but not even error console log runs after assigning accountId through getFieldValue. Please tell me what am I doing wrong?

7
  • Are you using this component on the org home page? May 25, 2020 at 13:18
  • Yes. please note, the retreived ID is even printed by: console.log('new acc Id: ' + this.accountId); - see last line of code. Indicating that accountId (which is a dynamic param $accountId) has indeed been updated, which should trigger @wire, but it does not, no console logs run inside \@wire getRecord no error, nothing May 25, 2020 at 14:31
  • Its because the record id is not available on the home page, it is available only on the detail page of the object. May 25, 2020 at 14:34
  • Ok, then Why does the last line: console.log('new acc Id: ' + this.accountId); show the said record ID? I am getting it through lightning-input-field May 25, 2020 at 14:37
  • @Dextersecret I update my answer, you need two wire methods if you need to access another object than one in your object-api-name.
    – Klecool
    May 25, 2020 at 15:04

2 Answers 2

2

You dont need to use getFieldValue to get id from e.detail.value.

Change this line

this.accountId = getFieldValue(e.detail.value, OPP_ACCOUNTID_FIELD);

To

this.accountId = e.detail.value[0];

More info according to the docs

Use the event.detail.value property to retrieve the Id of the selected contact record on the lookup field. Although the Id is returned in an array, multi-select lookups are currently not supported. This example also uses the success event to return the saved record. For more information on the success event, see the lightning-record-edit-form documentation.

Refer

The reason why you didn't recognize that it's an array

let ids = ['someidsdfsdfsdf'];
console.log('ids => ' + ids); // Output: ids => someidsdfsdfsdf
console.log('ids => ', ids);  // Output: ids =>  ["someidsdfsdfsdf"]

To avoid this you can either use , instead of + in the log as in the second console log.

Note: It's a good idea to remove the unwanted logs after debugging.

2
  • 1
    Nailed it! Thanks Rahul, I was going mad.And I was just about to ask you for a doc reference, but you already got it, and we have learned that the array will sometime work for multi-select fields. Strange how when I console.log e.detail.value, it does not show that it is an array. May 25, 2020 at 15:51
  • 1
    @Dextersecret If there is only one element in the array and if you log in on the console, it shows that element without mentioning that its an array. I will update that as well in my answer. May 25, 2020 at 15:58
0
recordId = '';

@wire(getRecord, {
    recordId: '$recordId',
    fields: []
})
thisOpportunity({ error, data }) {
}

and the wire for account

accountId = '';

@wire(getRecord, {
    recordId: '$accountId',
    fields: [ ACCOUNT_NAME_FIELD ]
})
thisAccount({ error, data }) {
}
5
  • Thanks, but I need a Name, not Id. Neither one of these are provided by getRecord, it just does not run, no Console.logs are run inside @wire data result May 25, 2020 at 11:26
  • you said "and need to get its Account Name on field change. (Homepage context component)" the "Homepage" is the Homepage of which object?
    – Klecool
    May 25, 2020 at 12:02
  • It's a global context - living on a home tab May 25, 2020 at 12:10
  • The recordId is required, if it's not provided the wire method doesn't run.
    – Klecool
    May 25, 2020 at 13:12
  • It is there, it's even printed by: console.log('new acc Id: ' + this.accountId); Indicating that accountId (which is a dynamic param $accountId) has indeed been updated, which should trigger @wire, but it does not. no error, nothing. May 25, 2020 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .