If you have been enjoying the development of lightning components since each release is coming up with exciting features. I liked the Lightning:RecordEditFrom and have used it in various projects. While working on a piece of a project, I liked the idea of showing fields based on the page-layout so a System Administrator can control the fields in the custom component instead of creating a change request to developers.
I used Apex Metadata API to get the fields from the layout and was showing them dynamically on the component. You can make it more configurable by adding the custom metadata types to control the page-layout assignments for the record types too. Here I am going to explain a simple use case.
ABC Corp. is a retail company and they are looking for a way to update the primary contact details just by clicking a button on the account record page. For that requirement, we are going to use a custom component which is using the lightning:recordEditForm. Here we don’t want to hard-code the fields and want to allow a Salesforce Admin to add/remove fields from the view so we are going to leverage the standard page layouts here.
Following component will fulfill the ABC Corps need and they can easily update the primary contact details through this custom component. Page layout details will be parsed in the apex controller by using the Apex Metadata API and recordEditForm will render fields dynamically. Although it isn’t a foolproof solution, you can refer it in your development. Here is the working example.
https://gist.github.com/sfcure/845a0e75815f7c0d6c92baebcce5bc3d
If you have any suggestion/comments then please feel free to reach out to me.
By Naveen Sharma
February 11, 2019Page just displayed all fields and sections in 1 column without any styling. Am I missing anything?
By Naveen Sharma
February 15, 2019You need to check section.totalColumns value as we are using this to set the multipleColumn property.
By Naveen Sharma
February 27, 2019Is it possible to make this as responsive design
When I view this page in mobile, the two columns cramped.
By Naveen Sharma
March 1, 2019You will have to use smallDeviceSize, mediumDeviceSize, largeDeviceSize attributes on lightning:layoutItem.
By Naveen Sharma
May 28, 2019Hi,
Can you please also assist me to display page layout required fields using recordeditForm with lightning:inputField ?
By Naveen Sharma
May 30, 2019Hi , I tried doing the same . it was very helpful . but for some reason the required custom fields are not required .How to handle this
By Naveen Sharma
July 4, 2019Hi Naval,
am not fetching any custom fields in the UI. Please suggest.
By Naveen Sharma
July 5, 2019Is it possible for multiple record types ?
If yes what are the required changes we need to make ?
By Naveen Sharma
July 17, 2019nice work!, tell me if the field is marked as required on a page layout, will it be a required field on the component also?
By Naveen Sharma
July 21, 2019Yes, it will be.
By Naveen Sharma
July 19, 2019IS there any way to prepopulate some of the field.
By Naveen Sharma
July 21, 2019Yes, there is a way but in that case it will require another approach.
By Naveen Sharma
July 21, 2019Great work Naval
But i am having an issue here, Every time when i pass the page layout name it returns me null so i am unable to proceed further. Do you have any idea why it is not working or salesforce just updated the API
Thanks!
By Naveen Sharma
July 21, 2019Make sure that you are passing a correct name. If Opportunity has a page layout name “Opportunity Sales Layout” then you need to pass it like. “Opportunity-Opportunity Sales Layout”.
By Naveen Sharma
August 9, 2019Hi Naval,
Good Blog.
Do you have code for pre-populate the same fields and save it in database?
By Naveen Sharma
September 4, 2019Thanks Naval for sharing this awesome post. Works as specified. I’m working on some minor improvements and will share the finished code when complete. You saved a few days of head scratching to get this started.
By Naveen Sharma
January 5, 2020HI Naval,
I could use this in opportunity cloning(with products) where I have to get all the fields in the layout and the corresponding values to pre-populate in the fields. What would be the approach to pre-populate values in the corresponding fields? Appreciate a quick response, I have a three days deadline 🙁
Thanks
Manju
By Naveen Sharma
January 8, 2020Hi Manju,
I think you may have to look at the following code where I tried to accomplish something similar.
https://github.com/sfcure/CloneOppUnamanged
By Naveen Sharma
January 29, 2020Hi Naval,
Great Blog.
What if i need to accumulate multiple layouts at once.
Can we do so like below?
List componentNameList = new List{‘Contact-Contact Layout’,’Contact-Contact Sales Layout’ };
Thanks in advance
By Naveen Sharma
January 30, 2020Hi Naval,
Nice work! nice work!, I used your approach and created the lightning component but the required fields that are on page layouts are not appearing on the lightning component as required.
Any one here have same issue?
By Naveen Sharma
March 31, 2020Make sure that it is required on the page layout.
By Naveen Sharma
February 13, 2020Hello Naval,
Great Work!!
how can we get a pagelayout name dynamically instead hard coding.
As far as i know we can not query a pagelayout using SOQL, so how we can pass pagelayout dynamically based on profile or may be user?
By Naveen Sharma
March 31, 2020Hi Mayank,
Either user Metadata API or Custom Label/Custom Metadata Types for the mapping.
By Naveen Sharma
July 26, 2020I’m getting sections which doesnt have field in it? is there anyway to stop them ?
By Naveen Sharma
July 26, 2020Also, there is an issue when we have 1 column?
By Naveen Sharma
July 26, 2020I just did it if anyone has issue with columns you may use this in ur aura “!section.totalColumns >= 1”