How to find fields that are part of a Page Layout in Salesforce?

How to find fields that are part of a Page Layout in Salesforce?

Using the following Apex Code snippet, you can find the fields that are added as part of a page layout in Salesforce.

Sample Code:

List < Metadata.Metadata > pageLayouts = Metadata.Operations.retrieve(
    Metadata.MetadataType.Layout, new List < String > { 'Account-Account Layout' }
);
for ( Metadata.Metadata objPageLayout : pageLayouts ) {
    
    Metadata.Layout layout = ( Metadata.Layout )objPageLayout;
    
    for ( Metadata.LayoutSection section : layout.layoutSections ) {
        
        for ( Metadata.LayoutColumn column : section.layoutColumns ) {
            
            if (column.layoutItems != null) {
                
                for ( Metadata.LayoutItem item : column.layoutItems ) {
                    
                    System.debug( 'Field API Name is ' + item.field );
                    
                }
            }
        }
    }
    
}

Using the above sample Apex Code snippet, you should be able to retrieve or extract or export or find the fields that are added as part of a page layout in Salesforce.

Output:

Leave a Reply