function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Barbora OtcovskáBarbora Otcovská 

Custom Lightning component-recordviewform

Hello everyone, I am basically advanced administrator, but I am trying to become a developer (so please, imagine explaining your answer to a toddler, so that i could understand:)) and on one of our projects I came across this issue:
We have custom lightning coponent, that is supposed to display some chosen fields. Basically, the code is based on this https://developer.salesforce.com/docs/component-library/bundle/lightning-record-view-form/documentation article.

We need icons, to be displayed there (so we are basically displaying formula fields), BUT it looks like the formula field itself has to be added to given page layout in order to display it in the component....can anyone tell why is it like this and if there is some workaround?
Thank you so much:)
Nicola GrisctiNicola Griscti
Well I would be surprised if you are using lightning web components, are you first sure your team does not use this https://developer.salesforce.com/docs/component-library/bundle/lightning:recordViewForm/documentation ?

If you are using LWC instead of Aura... congrats and continue reading.. If you would like to display certain fields, you can just list them in a variable and output them.. like this: 
IN THE HTML FILE:
<lightning-record-view-form record-id={recordId} object-api-name={objectApiName}>
<template for:each={fieldsArray} for:item='field'>
<p key={field.index}><lightning-output-field id={field} field-name={field}></lightning-output-field></p>
</template>
</lightning-record-view-form>


IN THE JAVASCRIPT FILE:
import { LightningElement, api} from 'lwc';
export default class MyLightningComponent extends LightningElement {
get fieldsArray(){
return this.fields.split(',');
}
}


 
Nicola GrisctiNicola Griscti
Sorry, that javascript file should actually be more:
import { LightningElement, api} from 'lwc';
export default class MyLightningComponent extends LightningElement {

 @api fields = "MyFormulaField__c,OtherField__c";
 @api objectApiName = "Contact";
get fieldsArray(){
return this.fields.split(',');
}
}
Instead of listing manually the fields, you can also wire it to apex to return a list of fields.. which would involve using the @wire to import an apex method for use, maybe you can give the apex method the object name and it can query all the fields for that object, and return you a list of the field names, and then you can do similar to above with it essentially..