1

I am trying to get a form to render conditionally based on the Status value of a case (don't ask me why I'm doing this just trying to learn about attributes, @wire, etc.). Any help would be appreciated and thanks in advance.

Currently throwing this error:

enter image description here

Here is my code:

HTML

<template if:true{status = 'Closed'}>
    <lightning-edit-record-form
        record-id={recordId}
        object-api-name={objectApiName}
        fields={fields}
        columns="2"
        mode="edit"
        onsubmit={handleSubmit}>
    </lightning-edit-record-form>
</template>

JS

import { LightningElement, api, wire } from 'lwc';
import STATUS_FIELD from '@salesforce/schema/Case.Status';
import CASE_REASON_FIELD from '@salesforce/schema/Case.Reason';
import INTERNAL_COMMENTS_FIELD from '@salesforce/schema/Case.Comments';
import { getFieldValue, getRecord } from 'lightning/uiRecordApi';


export default class CloseCase extends LightningElement {
    @api recordId;
    @api objectApiName;

    caseFields = [STATUS_FIELD, CASE_REASON_FIELD, INTERNAL_COMMENTS_FIELD];


    handleSubmit(event){
        event.preventDefault();
        const caseFields = event.detail.fields;
        this.template.querySelector('lightning-edit-record-form').submit(caseFields);
    }

    
    @wire(getRecord, {recordId: '$recordId', fields: caseFields})
    case;

    get status() {
        return getFieldValue(this.case.data, STATUS_FIELD);
    }
  //  loadCase({ data }) {
   // this.status = getFieldValue(data, STATUS_FIELD)
   // }


}

**XML**

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
      <target>lightning__AppPage</target>
      <target>lightning__RecordPage</target>
      <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

export default class CloseCase extends LightningElement {
    @api recordId;
    @api objectApiName;
    currentstatus;

    caseFields = [STATUS_FIELD, CASE_REASON_FIELD, INTERNAL_COMMENTS_FIELD];


    handleSubmit(event){
        event.preventDefault();
        const caseFields = event.detail.fields;
        this.template.querySelector('lightning-edit-record-form').submit(caseFields);
    }

    
    @wire(getRecord, {recordId: '$recordId', fields: caseFields})
    case;

    get status() {
         if(this.case.status = 'Closed') {
           return currentstatus = false;
         } else {
             return currentstatus = true;
         }
    }
  //  loadCase({ data }) {
   // this.status = getFieldValue(data, STATUS_FIELD)
   // }


}
1
  • It looks like it is missing a equals on if:true if:true={isClosed}> (and as glls mentioned you should create a property). Try that, but otherwise I suggest you to move the if true to the first element (on your case the edit record form). Sep 10, 2021 at 17:32

1 Answer 1

0

2 things:

1.- you need to wrap your template in <template></template> tags - no expressions allowed.

<template>
    //other templates can go here
</template>

2.- your condition cannot be declared in the template tag as you could in aura.

a heads up, if(this.case.status = 'Closed') you are assigning a value to this.case.stataus, in order to compare against a value, you need to use == or === or != !== check Comparisons and logical operators

also, for simplicity, you can use a ternary operator instead of if/else statements

return (comparison) ? true : false;

so, create a getter (which you already have) and check for a property value (true/false) or use the property directly:

<template if:true={proeprty}>

You can check Migrate Expressions for well documented examples and usage

4
  • Thanks for your response. I have the HTML file setup I believe, but I am struggling with turning that status value of closed into a conditional attribute to be used. This is what I have. My idea is to create a new attribute that is a boolean and has a value set based on the value of status.
    – coinjar
    Sep 10, 2021 at 18:09
  • updated my answer, you need to compare, not assign values.
    – glls
    Sep 10, 2021 at 18:26
  • I still cannot get this for the life of me. Currently I am using a comnnected callback
    – coinjar
    Sep 15, 2021 at 7:34
  • you need to add another set of <template> tags around your existing ones. with no conditions whatsoever
    – glls
    Sep 15, 2021 at 13:16

You must log in to answer this question.

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