0

I have a lightning-record-edit-form for Job__c with a lookup input to Labor_Category__c. Once a Labor_Category__c record is selected, I'd like to obtain the Labor_Category__r.Exempt__c value to then display. The getFieldValue() I'm using is just returning the Labor_Category__c record ID. I'd like to achieve this without having to call a custom apex method. Any ideas?

<lightning-record-edit-form object-api-name="Job__c" record-id={recordId}>
            <lightning-input-field field-name="Labor_Category__c" onchange={handleChange} value={labCat}></lightning-input-field>
 </lightning-record-edit-form>

JS:

import { LightningElement, api, track } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getFieldValue } from 'lightning/uiRecordApi';

import EXEMPT_FIELD from '@salesforce/schema/Labor_Category__c.Exempt__c';

export default class customLWC extends NavigationMixin(LightningElement) {
labCat;
labCatE;
handleChange(event){
        this.labCat = event.target.value;
        this.labCatE = getFieldValue(this.labCat, EXEMPT_FIELD);
        
        console.log('value = '+this.labCatE);
    }
}

1 Answer 1

1

In handleChange method event.target.value and therefore this.labCat hold just the Id of the record, so you cannot pass it to getFieldValue(record, field), because it takes a whole record as first parameter.
The only line needed in handleChange is the one that store event.target.value:

handleChange(event) {
    this.labCat = event.target.value;
}

Since you already defined an instace variable for the Labor_Category__c id and you're setting it in handleChange, you could use it with the wire adapter getRecord from uiRecordApi to retrieve the Exempt__c field value.
To import that adapter change import { getFieldValue } to import { getRecord, getFieldValue }.
Then you could use the wire adaper:

@wire(getRecord, { recordId: '$labCat', fields: [EXEMPT_FIELD] })
fetchLaborCategory({ data, error }) {
    if (data) {
        this.labCatE = getFieldValue(data, EXEMPT_FIELD);
    } else if (error) {
        // do something
    }
}

It will work because the wired properties, the ones with $ as prefix, are reactive, so the wired adapter is called as soon as their value change.

The whole JS file would look like:

import { LightningElement, api, track } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

import EXEMPT_FIELD from '@salesforce/schema/Labor_Category__c.Exempt__c';

export default class customLWC extends NavigationMixin(LightningElement) {
    labCat;
    labCatE;

    @wire(getRecord, { recordId: '$labCat', fields: [EXEMPT_FIELD] })
    fetchLaborCategory({ data, error }) {
        if (data) {
            this.labCatE = getFieldValue(data, EXEMPT_FIELD);
        } else if (error) {
            // do something
        }
    }

    handleChange(event) {
        this.labCat = event.target.value;
    }
}
1
  • Ruben you dog, appreciate it Oct 14, 2022 at 18:51

You must log in to answer this question.

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