I got requirement from my team where they wanted list of fields which are used or placed on page layout. They wanted to extract fields without any App Exchange product or tool.
For this requirement i have used Metadata API and Tooling API in apex to get list of all field by specifying page layout. Below are steps which we have to perform for getting fields
- Get Org objects
- Get Layout of object
- Get field of Page Layout
- Show above object, layout and fields information on Lightning Web Component
- Download fields as CSV File
1. Get Org objects
First we need list of all objects for which page layouts will be shown. We can get complete list of objects using Describe API. We can get standard as well custom objects of Salesforce Org.
for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){ system.debug(objTyp.getDescribe().getName()); }
2. Get Layout of object
Now we have list of all objects, let us get layouts of these object. We can get layout with specific object. We have to use Tooling API to get list of page layout for specific object. We can use ProfileLayout object like below SOQL for getting object’s page layout.
string sql='select Layout.Name from ProfileLayout where TableEnumOrId=\'objectName\'';
3. Get fields of Page Layout
Based on above two steps we can get fields of page layout. We can use Metadata API to get fields of any specific page layout.
string layoutName=String.format('{0}-{1}', new String[]{objectName, layout}); List<Metadata.Metadata> layouts = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, new List<String> {layoutName});
4. Show object, layout and fields information on Lightning Web Component
Now let us use above steps to show information on Lightning Web Component. We will add two combo-box, one for displaying object name and other for layout of selected object name.
We also have to add lightning-datatable in which we will show list of all fields for selected object and layout.
5. Download fields as CSV File
We can download field information as csv or excel. We can use text/csv mime type for downloading as CSV file.
let csvContent = "data:text/csv;charset=utf-8,"; csvContent +="Fields,\r\n"; //header this.fields.forEach(function(rowArray) { let row = rowArray.value+","; csvContent += row + "\r\n"; }); var encodedUri = encodeURI(csvContent); var link = document.createElement("a"); link.setAttribute("href", encodedUri); link.setAttribute("download", this.layoutName+".csv"); document.body.appendChild(link); link.click();
Important Pre-requisites
- By default we can’t call Tooling API directly with Apex using UserInfo.getSessionId(). We have to create Connected App, Auth Provider and Named Credential for this. Please refer blog https://salesforcecodex.com/2020/05/call-tooling-api-from-lightning-web-component/ for this.
- We have created named credential ToolingRest for this blog.
Complete Code:
1. Apex Code
2. Lightning Web Component
Test Page:
References:
https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/
https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_intro.htm
Related Posts
Salesforce DevOps for Developers: Enhancing Code Quality and Deployment Efficiency
Apex Code Coverage In Custom Object
Get All Used Custom Metadata Detail
Find Referenced Metadata using Salesforce Dependency API
Extract list of all fields from Page Layout
Field Access Explorer In lightning Web Component
Call Tooling API from Lightning Web Component
10 comments
very much helpful infirmation
Thank You Anil.
Regards
Dhanik
Hi Dhanik,
I am unable to fetch Page layout details, can you tell how can i troubleshoot the same.
Thanks in advance
Hello Ankit,
What error your are getting? Share your screenshot to salesforcecodex@gmail.com .
Thank You,
Dhanik
Hi,
I have used tooling API to get the validation rules for a specific object. I am stuck in how to show the response in a table in Lightning Component.
Any help will be much appreciated.
Hello Sushanta,
Please share your response data, based on that we can help you. Meanwhile you can check our other post for showing data in table https://salesforcecodex.com/2020/05/get-all-used-custom-metadata-detail/https://salesforcecodex.com/2020/05/get-all-used-custom-metadata-detail/.
Thank You,
Dhanik
This is awesome. The only thing is it only calls up Page Layouts from Standard objects, and not for Custom objects (with __c ending). Any ideas why?
Hey,
In the case of a custom object, please try to get using object id instead of name (TableEnumOrId).
Thank You,
Dhanik
How ProfileLayout is used? I could not query the layout. Please help me on getting layout for an object
Hello Silpa,
You can use Layout object to retrive all Layout details of object.
Thank You,
Dhanik