BUSINESS CHALLENGE
In Salesforce the number of fields per Object may vary from a few to many depending on the requirement.
We have both required and optional fields for every Object. The required fields should be filled in to save the record, but the optional fields need not be.
The optional fields may be present at the bottom of the detail page in a separate section or they maybe spread across the page
What if you have many fields and you are tired of scrolling down searching for them?
Although we can group relevant fields into sections, we still have to scroll down if there are significantly more number of fields.
We have a different approach to solve this issue.
We can use a Lightning Web Component, include the optional fields we want and add it to a new tab of the record page. You can switch between tabs and fill in the fields.
STEPS TO ACHIEVE THIS USING VS CODE
- Create a Project
- Create a Lightning Web Component
- Connect to Org and deploy
- Add the Lightning Component to a new tab in the record page
CREATE A PROJECT
-
Open VS Code and press ctrl + shift + p in Windows and cmd + shift + p in Mac to open the command palette.
-
Search for SFDX: Create Project and select it.
-
Select Standard project template, enter the project name, hit enter and select a folder in your system to save the project.
CREATE A LIGHTNING WEB COMPONENT
-
Open command palette and search for SFDX: Create Lightning Web Component and select it.
-
Enter the desired file name and hit enter.
-
The Component folder will have 3 files included by default. The JavaScript file ( .js ) , HTML file ( .html ) and the JavaScript Meta-XML file ( .js-meta.xml )
-
The modified JavaScript code for the Component looks like this
import { LightningElement,api } from 'lwc'; import Additional_Revenue_Proposed from '@salesforce/schema/Opportunity.Additional_Revenue_Proposed__c'; import Estimated_Revenue_Proposed from '@salesforce/schema/Opportunity.Estimated_Revenue_Proposed__c'; import Recurring_Non_Recurring_Proposed from '@salesforce/schema/Opportunity.Recurring_Non_Recurring_Proposed__c'; import Total_Revenue_Proposed from '@salesforce/schema/Opportunity.Total_Revenue_Proposed__c'; import Additional_Revenue_Awarded from '@salesforce/schema/Opportunity.Additional_Revenue_Awarded__c'; import Estimated_Revenue_Awarded from '@salesforce/schema/Opportunity.Estimated_Revenue_Awarded__c'; import Recurring_Non_Recurring_Awarded from '@salesforce/schema/Opportunity.Recurring_Non_Recurring_Awarded__c'; import Total_Revenue_Awarded from '@salesforce/schema/Opportunity.Total_Revenue_Awarded__c'; export default class OpportunityTab extends LightningElement { @api recordId; fields = [Additional_Revenue_Proposed, Estimated_Revenue_Proposed, Recurring_Non_Recurring_Proposed, Total_Revenue_Proposed, Additional_Revenue_Awarded, Estimated_Revenue_Awarded, Recurring_Non_Recurring_Awarded, Total_Revenue_Awarded] }
-
The fields we use in the Component should each be imported from @salesforce/schema/.
-
Here is the HTML code for the Component
<template> <lightning-card> <lightning-record-form record-id={recordId} object-api-name="Opportunity" fields={fields} density="comfy" columns="2" > </lightning-record-form> </lightning-card> </template>
-
The lightning-record-form we use here switches between view and edit modes automatically when the user begins editing a field in a view form. It also provides Save and Cancel buttons by default in edit mode.
-
In the lightning-record-form you can choose to specify how many number of columns you want to split the fields into.
-
The density can be either comfy or compact for the form. The display density setting determines how densely content is displayed and where field labels are located.
-
Here is the code for the JavaScript Meta-XML file
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>52.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__RecordPage</target> </targets> </LightningComponentBundle>
-
The target here can be RecordPage, HomePage or AppPage depending on the requirement. You can also target all three at the same time.
CONNECT TO ORG AND DEPLOY
-
Open Command palette and search for SFDX: Authorize an Org and select it.
-
Choose your Org type and if it is Custom, paste your Orgs login URL in the next step.
-
For Org alias, hit enter for a default alias or type a new alias.
-
On hitting enter you will be taken to the Salesforce Login Page in a browser.
-
Enter the Credentials and Login.
-
Head back to VSCode and you will see the notification SFDX: Authorize an Org successfully ran.
-
Now right click on the folder opportunityTab folder which is on the left pane and click SFDX: Deploy Source to Org.
-
You should see the notification SFDX: Deploy source to Org successfully ran shortly.
ADD THE LIGHTNING COMPONENT TO A NEW TAB IN THE RECORD PAGE
-
If you do not have Tabs component added to the record page, go to any Opportunity record page and click the gear icon at the top right corner and select edit page.
-
On the left pane under Components, drag and drop the Tabs component from the Standard section onto the page.
-
The Tabs component will have Details and Related tabs by default.
-
Click on the Details tab, from the left pane drag and drop the Record detail standard component into it.
-
For the Related tab drag and drop any of the standard related components that you require.
-
Now select the entire Tabs component.
-
On Selecting the Component, you can see the different Tabs present in the Component on the right pane.
-
Click on Add Tab.
-
The Details tab will be chosen by default. Click on it and you will see a popup for the Tab Label.
-
From the picklist values of the Tab Label, scroll to the top and select Custom.
-
Now enter the Custom Label and click Done.
-
The new tab is added to the layout.
-
From the left pane drag and drop the custom Component opportunityTab into the new tab created.
-
You will be able to see the preview of the custom Component.
-
Click Save at the top right corner and then Activate the page.
-
Assign as Org Default, App Default or App, Record Type and Profile based on your requirement.
-
If it is Org Default, select the form factors that you want your Org Default page to be available for. click Next and then click Save.
-
If it is App Default, select the App to display the page as the App default page. Click Next and then click Save.
-
If it is App, Record Type and Profile, Select the App, form factor, Record Types, Profiles and then Click Save.
-
Now head back to any Opportunity record page and you will be able to see the new tab.
WRAPPING IT UP
In this blog we have discussed how to add the fields of an Object to a new tab of the Record page using a Lightning Web Component.
Leave a Comment