Introduction

The main concept showcased in this recipe is the lightning-record-view-form component, allowing the creation of a form that displays read-only fields associated with a record using their labels in the language of the user.

<aside> 📚 Official documentation https://developer.salesforce.com/docs/component-library/bundle/lightning-record-view-form/specification

</aside>

<aside> 📁 Find this LWC in this folder: force-app\\main\\default\\lwc\\recordViewFormStaticContact

</aside>

<aside> ☝ Review this page recordId and objectApiName special properties and Accessing Data with LWC, which are the basics for understanding this LWC.

</aside>

Static vs Dynamic

There are 2 LWCs:

  1. The statically imported version uses the full import ACCOUNT_FIELD from '@salesforce/schema/Contact.AccountId' path referenced to the Salesforce schema in the Javascript file and then in the template is included like this <lightning-output-field field-name={accountField}> .
  2. The dynamically imported version**:** there is no import as such in the Javascript file, and in the template file the field is included using its name directly <lightning-output-field field-name="AccountId"> .

For both:

This LWC can be placed on a page where a contact record is displayed, and this is the reason we need to go to the Contacts Tab.

Advantages of static vs dynamic

In the static LWC, the field names are specified individually, which provides error checking during development time but is less flexible:

Template file for the static version

Several  lightning-output-field components are used inside the lightning-record-view-form to display the value of a record field.

<aside> 💡 Read more about the recordId and objectApiName variables here: recordId and objectApiName special properties

</aside>

<div class="slds-var-m-around_medium">
    <lightning-record-view-form
        object-api-name={objectApiName}
        record-id={recordId}>

        <lightning-output-field field-name={accountField}></lightning-output-field>
        <lightning-output-field field-name={nameField}></lightning-output-field>
        ...
    </lightning-record-view-form>
</div>

Javascript file for the static version