Home SalesforceApex Dynamic Record Page Creation using FieldSet

Dynamic Record Page Creation using FieldSet

by Dhanik Lal Sahni

We usually get the requirement to develop a lightning page for creating records in the Salesforce object. We have to create different components for creating different kinds of object records. Instead of creating multiple components, we can utilize FieldSet to create dynamic pages. This post will explain dynamic record page creation using a fieldset.

Fieldset is group of fields and used to add, remove and reorder fields. FieldSets are used to style forms and display those logical associations between fields. FieldSets are used to display fields.

Let us see how we can create the dynamic page using Fieldset. The below steps are required to create a dynamic page

  1. Create an apex class to get fields from the fieldset
  2. Create a generic lighting component
  3. Test Component

1. Create an apex class to get fields from the fieldset

Let us create an apex class that will get field details based on the given field set and object. We will use Schema.DescribeSObjectResult, Schema.getGlobalDescribe and Schema.FieldSet to get field details. These will be used for describing sObject.

After getting object descriptions from the above-mentioned classes, we will prepare a list of field information. This list will be used in LWC component to create the page.

Code for getting field detail for a given object and fieldset

2. Create a generic lighting component

Create a re-usable LWC component that will use the above apex class and lightning-record-edit-form to generate the layout. By default, it will display controls in 1 control per row, if you want to show 2 or 3 columns per row then use a lightning design system to generate multiple columns. slds-col slds-size_1-of-2 style will convert rows into 2 columns layout.

We need to get the object name, field set, or record id to display the page. So create public properties for this information.

Public properties that are required are

  1. Record ID
  2. Object Name
  3. FieldSet Name

3. Test Component

To test the component, create a field set in 2 different objects. Add the above-created component on the record page or home page in Lightning.

FieldSet in Account Page:

Configure Component:

Test Video:

References:

About Field Sets

Other similar posts:

Adding table row dynamically in Lightning Web Component

You may also like

2 comments

Ankit November 4, 2023 - 11:35 pm

will it work with Lookup fields too?

Reply
Dhanik Lal Sahni November 7, 2023 - 7:55 am

Yes Ankit, It will work as we are using lightning-record-edit-form.

Thank You,
Dhanik

Reply

Leave a Comment