1

lightning-record-view-form is given a object type and a recordId had retrieves data for that type for the record with the Id. Then tags like lightning-output-field can reference field values. What I'd like to do is reference field values from the record in various expressions. Anyone know if there's a way to do that? Example of what I'm trying to do below....

<lightning-record-view-form record-id= {recordId} object-api-name="Foo__c">
  <lightning-output-field field-name="Fee__c"></lightning-output-field>
  <template if:true={*some expression that Foo__c.Fum__c has a value*}>
    <lightning-output-field field-name="Fum__c"></lightning-output-field>
  </template>
  ...

1 Answer 1

2

My proposition is do to something like that:

About solution

You cannot have a direct expression in if:true like if:true={something=someValue} it needs to be in .js file. It forces you to create a getter (get) or just use some component property/variable. You also need to get record data that can be quite easily retrieved via uiRecordApi without apex.

Code

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

import FUM from '@salesforce/schema/Foo__c.Fum__c';

const FIELDS = [FUM];

export default class Example extends LightningElement {

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

get isFumEqualSomeValue() {
    return getFieldValue(this.foo.data, FUM) === 'some value';
}

<lightning-record-view-form record-id= {recordId} object-api-name="Foo__c">
  <lightning-output-field field-name="Fee__c"></lightning-output-field>
  <template if:true={isFumEqualSomeValue}>
    <lightning-output-field field-name="Fum__c"></lightning-output-field>
  </template>
  ...

Resources:

1
  • 1
    Nice, exactly what I was going to say.
    – sfdcfox
    Oct 6, 2022 at 20:24

You must log in to answer this question.

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