Skip to content

Instantly share code, notes, and snippets.

@mickle00
Last active August 29, 2015 14:03
Show Gist options
  • Save mickle00/2b0d89a6c2871f884a7b to your computer and use it in GitHub Desktop.
Save mickle00/2b0d89a6c2871f884a7b to your computer and use it in GitHub Desktop.
ApexMetadata API Samples
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.Layout layout = (MetadataService.Layout) service.readMetadata('Layout', new String[] { 'Account-Account Layout' }).getRecords()[0];
system.debug(JSON.serializePretty(layout.layoutSections[0].layoutColumns[0].layoutItems[0].field));
List<MetadataService.ListMetadataQuery> queries = new List<MetadataService.ListMetadataQuery>();
MetadataService.ListMetadataQuery queryListView = new MetadataService.ListMetadataQuery();
queryListView.type_x = 'ListView';
queries.add(queryListView);
MetadataService.ListMetadataQuery queryLayout = new MetadataService.ListMetadataQuery();
queryLayout.type_x = 'Layout'; //'RecordType' also works
queries.add(queryLayout);
MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 25);
//for(MetadataService.FileProperties fileProperty : fileProperties) system.debug(fileProperty.fullName);
MetadataService.ListView listView =
(MetadataService.ListView) service.readMetadata('ListView',
new String[] { 'Contact.MyContacts' }).getRecords()[0];
system.debug(JSON.serializePretty(listView));
if(listView.columns!=null)
for(String column : listView.columns)
System.debug('Column ' + column);
if(listView.filters!=null)
for(MetadataService.ListViewFilter filter : listView.filters)
System.debug('Filter ' + filter.field + ' ' + filter.operation + ' ' + filter.value);
queryLayout.type_x = 'RecordType';
queries.add(queryLayout);
MetadataService.FileProperties[] fileProperties = service.listMetadata(queries, 25);
for(MetadataService.FileProperties fileProperty : fileProperties) system.debug(fileProperty.fullName);
MetadataService.RecordType recordType = (MetadataService.RecordType) service.readMetadata('RecordType',
new String[] { 'Contact.Person' }).getRecords()[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment