0

I have what I feel like ought to be a super simple Lightning Web Component. I am showing this on a Tab in a record page for a Case.

Complete JS:

import { LightningElement, api, wire, track } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import IS_CLOSED from '@salesforce/schema/Case.IsClosed';
const FIELDS = [IS_CLOSED];

export default class CaseCloseForm extends LightningElement {
    @api recordId;

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

    get isClosed() {
        return getFieldValue(this.record.data, IS_CLOSED);

    }

}

Complete HTML:

<template>
        Record ID: {recordId}<br/>
        Is Closed {isClosed}<br/>
</template>

Complete Metadata:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>50.0</apiVersion>
    <description>Lwc Close Case</description>
    <isExposed>true</isExposed>
    <masterLabel>Lwc Close Case</masterLabel>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

UI Output:

enter image description here

Note that "Is Closed" returns no information, true or false.

I know that @wire runs asynchronously so I tried updating my template:

<template  >
    <template if:true={record.data}>
        record.data is true
    </template>
    <template if:false={record.data}>
        record.data is false
    </template>
</template>

This returns record.data is false in the UI and does not change.

Is there anything special I need to do to get this to run on a record page in a tab? What am I missing? I'm sure it's something simple. Thanks in advance.

1 Answer 1

3

JavaScript is cAsE-sEnSiTiVe. In this case, you tried to specify a parameter called FIELDS to getRecord, but it should have been fields.

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

When you specify a field from a variable:

let x = 5;
let y = { x };

You end up with a property of the same name and the value from the variable:

// y = { x: 5 }

Since you're supposed to all-caps a constant variable (this is definitely a good practice), you can't use the shorthand notation for the property.

6
  • Thank you for the quick reply, this is helpful, but I am not entirely sure I follow. I changed the value in the line from fields: FIELDS to fields: fields and now I get an error: ` [fields is not defined]`. My original post followed the pattern as specified in this this prior post: salesforce.stackexchange.com/questions/305264/… - practically speaking what do I need to change? Jan 25, 2021 at 3:39
  • @BobHatcher it should be fields: FIELDS, as in my answer. You wrote {recordId:'$recordId', FIELDS }.
    – sfdcfox
    Jan 25, 2021 at 3:45
  • Thank you - the line now reads @wire(getRecord, fields: FIELDS), but the compiler is complaining ("LWC1503: Parsing error: The type cast expression is expected to be wrapped with parenthesis"). But I still feel like I'm missing something - in the link above, Mert Yalti does it exactly as I did in my original post and it is an accepted answer. Was my way wrong? Jan 25, 2021 at 15:28
  • @BobHatcher You're missing curly brackets now. Look at my code again.
    – sfdcfox
    Jan 25, 2021 at 15:39
  • I just changed it back to @wire(getRecord, { recordId: '$recordId', fields: FIELDS }) as in my original post and it is now working, I wonder if it was a browser cache issue or something else wonky going on. Jan 25, 2021 at 15:40

You must log in to answer this question.

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