Home Salesforce Extract list of all fields from Page Layout

Extract list of all fields from Page Layout

by Dhanik Lal Sahni

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

  1. Get Org objects
  2. Get Layout of object
  3. Get field of Page Layout
  4. Show above object, layout and fields information on Lightning Web Component
  5. 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

  1. 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. 
  2. 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

You may also like

10 comments

Gugulothu Anil May 14, 2020 - 3:50 pm

very much helpful infirmation

Reply
Dhanik Lal Sahni May 14, 2020 - 5:48 pm

Thank You Anil.

Regards
Dhanik

Reply
ankit May 31, 2020 - 5:37 pm

Hi Dhanik,

I am unable to fetch Page layout details, can you tell how can i troubleshoot the same.

Thanks in advance

Reply
Dhanik Lal Sahni June 1, 2020 - 8:24 pm

Hello Ankit,

What error your are getting? Share your screenshot to salesforcecodex@gmail.com .

Thank You,
Dhanik

Reply
Sushanta Karan June 23, 2020 - 11:04 pm

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.

Reply
Dhanik Lal Sahni June 25, 2020 - 12:17 am

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

Reply
Cyu November 11, 2021 - 8:20 pm

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?

Reply
Dhanik Lal Sahni November 25, 2021 - 2:23 pm

Hey,

In the case of a custom object, please try to get using object id instead of name (TableEnumOrId).

Thank You,
Dhanik

Reply
Silpa October 20, 2023 - 10:52 am

How ProfileLayout is used? I could not query the layout. Please help me on getting layout for an object

Reply
Dhanik Lal Sahni October 28, 2023 - 10:04 pm

Hello Silpa,
You can use Layout object to retrive all Layout details of object.

Thank You,
Dhanik

Reply

Leave a Comment