0

I'm trying to get fields from a record to display in a lightning web component that goes on a record page. {recordId} works but {hasDirectReports} and other fields don't. Should be simple - I'm not sure what I'm doing wrong though

HTML:

        <div class="slds-text-heading_medium slds-p-bottom_small">
            <center> <h2> {recordId} {hasDirectReports} _</h2> </center>            
        </div>

JS:

import { LightningElement, api, track, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import HAS_DIRECT_REPORTS_FIELD from '@salesforce/schema/Job_Description__c.Has_Direct_Reports__c';

const FIELDS = [
    'HAS_DIRECT_REPORTS_FIELD',
];

export default class JobDescriptionContainer extends LightningElement {
    @api recordId;
    @track hasDirectReports;

    @wire(getRecord, {recordId: '$recordId', fields: FIELDS })
    jobDescription;

    get hasDirectReports() {
        return this.jobDescription.data.fields.Has_Direct_Reports__c.value;
    }
    set hasDirectReports(value) {       
        this.hasDirectReports = value;
    }

*Has_Direct_Reports__c is a checkbox field, although I don't think that's causing the issue

1 Answer 1

0

You have a number of minor problems.


const FIELDS = [
    HAS_DIRECT_REPORTS_FIELD,
];

There should be no quotes around the constant imported for this field.


@track hasDirectReports;

...

get hasDirectReports() {
    return this.jobDescription.data.fields.Has_Direct_Reports__c.value;
}

You would use only one or the other, not both. I recommend removing:

@track hasDirectReports;

The getter should automatically update the value as the wire service returns data. Also, consider just using getFieldValue:

import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

get hasDirectReports() {
    return getFieldValue(this.jobDescription.data, HAS_DIRECT_REPORTS_FIELD);
}

Examples are in the documentation.

0

You must log in to answer this question.

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