Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Extract list of all fields from Page Layout

    Extract list of all fields from Page Layout

    Dhanik Lal SahniBy Dhanik Lal SahniMay 13, 2020Updated:June 11, 202310 Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Extract list of all fields from Page Layout
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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:

    https://salesforcecodex.com/wp-content/uploads/2020/05/Extract-Fields-from-Page-Layout.crdownload.mp4

    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

    apex Lightning web component lightning-datatable lwc metadata api named credential salesforce tooling api
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleCall Tooling API from Lightning Web Component
    Next Article Find Referenced Metadata using Salesforce Dependency API
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    View 10 Comments

    10 Comments

    1. Gugulothu Anil on May 14, 2020 3:50 pm

      very much helpful infirmation

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

        Thank You Anil.

        Regards
        Dhanik

        Reply
    2. ankit on 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 on June 1, 2020 8:24 pm

        Hello Ankit,

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

        Thank You,
        Dhanik

        Reply
    3. Sushanta Karan on 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 on 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
    4. Cyu on 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 on 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
    5. Silpa on 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 on 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 Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.