Skip to content

Instantly share code, notes, and snippets.

@jongpie
Forked from Feldstrom/LayoutDescriber.cls
Last active September 21, 2022 02:49
Show Gist options
  • Save jongpie/006fb76f99418b03df54b9ff566a9911 to your computer and use it in GitHub Desktop.
Save jongpie/006fb76f99418b03df54b9ff566a9911 to your computer and use it in GitHub Desktop.
Apex script for pulling field names from specific page layouts
public String objectName = ''; //example 'Case'
public String layoutName = ''; //example: 'Case Layout'
new LayoutDescriber().run(objectName, layoutName);
// In anonymous Apex, you can define & run a class.
// In this script, using a class ensures all describe methods work, regardless of field-level security (FLS)
public without sharing class LayoutDescriber {
public void run(String objectName, String layoutName) {
layoutName = objectName + '-' + layoutName;
List<String> layoutFieldsCollection = new List<String>();
List<Metadata.Metadata> layouts = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, new List<String> {layoutName});
Metadata.Layout layoutMd = (Metadata.Layout)layouts.get(0);
for (Metadata.LayoutSection section : layoutMd.layoutSections) {
for (Metadata.LayoutColumn column : section.layoutColumns) {
if (column.layoutItems != null) {
for (Metadata.LayoutItem item : column.layoutItems) {
if (item.field != null){
layoutFieldsCollection.add(item.field);
}
}
}
}
}
for (String s : layoutFieldsCollection){
if (String.isNotBlank(s)){
System.debug('Field Label, ' +
Schema.getGlobalDescribe()
.get(objectName)
.getDescribe()
.fields.getMap()
.get(s)
.getDescribe()
.getLabel()
+ ',');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment