I'm trying to write a really simple component that takes the field names from the design attributes and displays only the field values, not the standard lightning-output-field.
There are two(?) ways to approach this, getRecord or return the lightning-record-form
and OnLoad do some Javascript to get the field values.
I tried @sfdcfox's idea here using getRecord but no matter what I did the '$fieldsFormatted'
would not work ('$fieldsFormatted'
returned the exact array needed, and hard coding the array returned the exact data needed, but the two together just returned undefined).
I have gotten almost there with the lightning-record-form
this great example but I am missing the last two steps (returning the field value without specifying the name, and displaying the field value). Getting the Field Value came from this example and both were from @salesforce-sas.
HTML
<template>
<lightning-card title="Design Attribute Demo" icon-name="custom:custom19">
<template if:true={fieldsFormatted}>
<lightning-record-form record-id={recordId} object-api-name={objectApiName} mode="view"
fields={fieldsFormatted} onload={handleOnLoad}>
<template for:each={fieldsFormatted} for:item="fld">
<lightning-output-field key={fld} field-name={fld}></lightning-output-field>
<p key={fld}>{fValue}</p> <!-- Q2: Does not render -->
</template>
</lightning-record-form>
</template>
</lightning-card>
</template>
JS
import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class DesignerFields extends LightningElement {
@api indFields = 'Industry;NumberOfEmployees';
@api objectApiName = 'Account';
@api recordId;
@api record;
fieldsFormatted = ['Id'];
connectedCallback() {
this.fieldsFormatted = (this.indFields || 'Id').split(';').map(field => field);
console.log(JSON.stringify(this.fieldsFormatted));
}
handleOnLoad(event) {
const fValue = event.detail.records[this.recordId].fields.Industry.value;
//Q1: fValue = "Education" the correct value. But "Industry" Field Name is still hard coded.
//How do I get the value for the field in {fld} variable?
//Q2: How do I get fValue to render back in the HTML instead of the lightning-output-field
//I tried this.dispatchEvent(fValue); and it had an error
}
}
Meta
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property name="indFields" label="Field Names" type="String" required="true" default="Industry;NumberOfEmployees" description="The Field Names." />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
How do you suggest I get the value rendered instead of showing the lightning-output-field please?