Learn About Lightning-record-form Component in Salesforce

recordForm

With the lightning-record-form component, forms to add, view, or update records may be made quickly. 

Creating record forms with this component is simpler than manually developing forms using lightning-record-edit-form or lightning-record-view-form. The functionality offered by the lightning-record-form component is as follows: 

Automatically switches between view and edit modes when a user starts editing a field in a view form. 

Automatically adds cancel and save buttons to edit forms. 

Utilises the default record layout for the object, which supports multiple columns. 

Loads the specified fields, or the entire compact or full layout of the object. 

Lightning-record-form, however, offers less customization. Use lightning-record-edit-form (add or update a record) and lightning-record-view-form to configure the form layout and provide a custom presentation of record data (view a record). 

When changing or viewing a record, only the record-id is necessary; the object-api-name attribute is always required. 

Lightning Data Service is implemented by lightning-record-form, which eliminates the need for extra Apex controllers to add or change record data. Additionally, it handles field-level security and sharing so that users only see the data to which they have access. Use Lightning Data Service wire adapters and functions, such as those from the lightning/ui*Api module, to access raw record data or to develop forms that require more customisation than what the lightning-record-*-form components can provide. 

Don’t forget to check out: What is Lightning Notification Library in Salesforce

Object API Name and Record ID

A Salesforce object is connected to every Salesforce record. For instance, the Contact object is connected to a contact record. Prefixes are used when creating record IDs to identify the object. To create the connection between a record and an object, you must specify the object-api-name attribute in the lightning-record-form component. The object API name needs to be suitable for the component’s intended use. Set object-api-name=”Account” for instance, if lightning-record-form is present on a record page for an account. Only when the record ID and the given object API name agree does the component submit updates. Users get a message saying the API name is invalid if there is a mismatch. 

Specifying Object API and Field Names 

When importing references to objects and fields, especially if you intend to distribute your component as a managed package, we strongly advise utilising the @salesforce/schema syntax. 

For example, 

<lightning-record-form 
    object-api-name={objectApiName} 
    record-id={recordId} 
    fields={fields} 
> 
</lightning-record-form>

Import a reference to the name field of the contact object. In this example, the form uses the object API name and record ID of the record page it’s located on.

import { LightningElement, api } from 'lwc'; import NAME_FIELD from '@salesforce/schema/Contact.Name';
export default class RecordFormExample extends LightningElement {
    // Expose a field to make it available in the template
    fields = [NAME_FIELD];
    // Flexipage provides recordId and objectApiName
    @api recorded;
    @api objectApiName;
}

Supported Objects

This component does not support all standard Salesforce objects. For example, Event and Task objects are not supported. This limit also applies to records referencing fields belonging to unsupported objects. 

External objects are not supported. Editing the InformalName field is not supported. See the User Interface API Developer Guide for a list of supported objects. 

If you are working with unsupported objects or need to query specific records using SOQL, create your own UI and use Apex to work with Salesforce data. For more information, see Data Policy in the Lightning Web Components.  

Developer Guide. 

Modes

Edit Create editable forms to add records or update existing records. Specify the record ID when updating an existing record. Edit mode is the default if no record ID is specified, and displays a form for creating a new record. Views Create forms for viewing records and also allow users to edit them. Each record field has an edit button. Display mode is the default when a record ID is specified. readonly Creates a form that displays records without allowing editing. The form does not display any buttons. 

Specifying Record Fields

In all modes, the component expects fields or layout-type attributes. 

Use the fields attribute to pass record fields as an array of strings. Fields are displayed in the order listed. 

Use the layout-type attribute to specify a full or compact layout. Layouts are typically defined (created and modified) by an administrator. When you specify record data using a layout type, fields are loaded into the layout definition. All fields mapped to the layout are loaded into the form. This behaviour is the same as his GetRecordUi wire adapter for Lightning Data Services. 

dont miss out iconCheck out another amazing blog by Navdita here: Dashboards in Salesforce – Get All the Details About it Here

To display fields for the organization layout type: 

 Full – Full layout corresponds to the fields on the record detail page. Go to Page Layouts in the admin settings for the object you want to edit. Compact – Compact layout corresponds to the fields in the highlight area at the top of the record. Go to compact layout in the admin settings for the object you want to edit. See Page Layout for more information. 

A Lightning Record form displays a field as required only if the object marks the field as required. If a field is marked as required on only a one-page layout, the form will not render the field using the required field styles or validations. To specify the order of fields, use fields without the layout-type attribute. We don’t recommend using the fields attribute with the layout-type attribute because the fields might display in a different order. Or display a custom layout with the Lightning Record Edit Form or Lightning Record View Form components. 

Fields with global relationships aren’t supported on Lightning record forms. The specified fields must map to the same object API name. Fields like Contact.Account.Ownership are cross-object references between Contact and Account objects and cannot be displayed on the form. 

 



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *