The main concept included in this recipe is to showcase the lightning-record-form
component that allows to display of a record form and also editing the fields.
<aside> 📚 Official documentation https://developer.salesforce.com/docs/component-library/bundle/lightning-record-form/specification.
</aside>
<aside>
☝ Review this page recordId and
objectApiName special properties
and Accessing Data with LWC, which are the basics for understanding this LWC.
</aside>
<aside>
📁 Find this LWC in this folder: force-app\\main\\default\\lwc\\recordFormStaticContact
and force-app\\main\\default\\lwc\\recordFormDynamicContact
</aside>
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.
There are 2 LWCs:
The statically imported version:
import ACCOUNT_FIELD from '@salesforce/schema/Contact.AccountId'
with the path referenced to the Salesforce schema and then declaring the fields
array fields = [ACCOUNT_FIELD, NAME_FIELD, TITLE_FIELD, PHONE_FIELD, EMAIL_FIELD]
.<lightning-record-form
element is used:<lightning-record-form
object-api-name={objectApiName}
record-id={recordId}
fields={fields}
></lightning-record-form>
In the dynamically imported version, a similar approach is followed:
fields
array is declared but using the name of the fields as fields = ['AccountId', 'Name', 'Title', 'Phone', 'Email']
.In the static LWC, the field names are specified individually, which provides error checking during development time but is less flexible:
fields
array dynamically.fields
values for the record that is informed using the recordId
property.object-api-name
and record-id
variables and optionally the list of fields
to be displayed.<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-form
object-api-name={objectApiName}
record-id={recordId}
fields={fields}
></lightning-record-form>
</div>
...
In the following code extract, it is clear why this LWC is named recordFormstaticContact
, as there is a bunch of import
statements to import the fields and assign them to the fields
array statically.
...
import ACCOUNT_FIELD from '@salesforce/schema/Contact.AccountId';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import TITLE_FIELD from '@salesforce/schema/Contact.Title';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
import EMAIL_FIELD from '@salesforce/schema/Contact.Email';
export default class RecordFormStaticContact extends LightningElement {
// Flexipage provides recordId and objectApiName
@api recordId;
@api objectApiName;
fields = [ACCOUNT_FIELD, NAME_FIELD, TITLE_FIELD, PHONE_FIELD, EMAIL_FIELD];
}
...
In the following screenshot, you can see the LWC in action, after the Department field was added.