Skip to content

Instantly share code, notes, and snippets.

@richardvanhook
Created January 13, 2017 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardvanhook/208492d53f656fcc2cef6c057bac6814 to your computer and use it in GitHub Desktop.
Save richardvanhook/208492d53f656fcc2cef6c057bac6814 to your computer and use it in GitHub Desktop.
/*
REQUIRES https://github.com/financialforcedev/apex-mdapi
Exports all instances of field-layout items into CSV format:
Object,Field,Layout,Behavior
"account","AccountNumber","account-account %28marketing%29 layout","Edit"
"account","AccountNumber","account-account %28sales%29 layout","Edit"
......
*/
final Set<String> SOBJECT_SCOPE = new Set<String>{
'ACCOUNT','Contact'
};
final List<String> rows = new List<String>();
final List<String> columns = new List<String>();
MetadataService.Layout layoutMetadata = null ;
String objectName = null ;
final List<String> layouts = listLayouts(SOBJECT_SCOPE);
for(MetadataService.Metadata m : getService().readMetadata('Layout',layouts).getRecords()){
layoutMetadata = (MetadataService.Layout) m;
objectName = splitReturnFirst(layoutMetadata.fullName,'-');
if(layoutMetadata.layoutSections != null) for(MetadataService.LayoutSection ls : layoutMetadata.layoutSections)
if(ls.layoutColumns != null) for(MetadataService.LayoutColumn lc : ls.layoutColumns)
if(lc.layoutItems != null) for(MetadataService.LayoutItem li : lc.layoutItems)
if(String.isNotBlank(li.field)){
columns.clear();
columns.add(objectName);
columns.add(li.field);
columns.add(layoutMetadata.fullName);
columns.add(li.behavior);
rows.add('"'+String.join(columns,'","')+'"');
}
}
rows.sort();
rows.add(0,'Object,Field,Layout,Behavior');
System.debug(String.join(rows,'\n'));
//================================================
// HELPER METHODS
//================================================
static MetadataService.MetadataPort getService(){
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
return service;
}
static String splitReturnFirst(String str, String separator){
String returnValue = null;
if(str != null){
List<String> split = str.split(separator,2);
if(split != null && split.size() > 0){
returnValue = split[0];
}
}
return returnValue;
}
static Set<String> lowerCase(Set<String> s){
Set<String> returnValue = null;
if(s != null){
returnValue = new Set<String>();
for(String str : s){
if(str == null) returnValue.add(null);
else returnValue.add(str.toLowerCase());
}
}
return returnValue;
}
static List<String> listLayouts(Set<String> scope){
scope = lowerCase(scope);
if(scope == null) scope = new Set<String>();
final List<String> returnValue = new List<String>();
final MetadataService.ListMetadataQuery q = new MetadataService.ListMetadataQuery();
q.type_x = 'Layout';
for(MetadataService.FileProperties layout : getService().listMetadata(
new List<MetadataService.ListMetadataQuery>{q},25
)){
if( scope != null
&& scope.size() > 0
&& !scope.contains(splitReturnFirst(layout.fullName,'-').toLowerCase())
){
continue;
}
returnValue.add(layout.fullName.toLowerCase());
}
returnValue.sort();
return returnValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment