Skip to content

Commit d5c86b8

Browse files
committed
Added new example for adding a field to a layout
1 parent 43640c7 commit d5c86b8

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

apex-mdapi/src/classes/MetadataServiceExamples.cls

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,34 @@ public with sharing class MetadataServiceExamples
737737
new MetadataService.Metadata[] { layout })[0]);
738738
}
739739

740+
741+
public static void addFieldToLayout()
742+
{
743+
MetadataService.MetadataPort service = createService();
744+
745+
// Read the Layout
746+
MetadataService.Layout layout =
747+
(MetadataService.Layout) service.readMetadata('Layout',
748+
new String[] { 'Test__c-Test Layout' }).getRecords()[0];
749+
750+
// Add Layout section, layout, item and field
751+
if(layout.layoutSections==null)
752+
layout.layoutSections = new List<MetadataService.LayoutSection>();
753+
MetadataService.LayoutSection newLayoutSection = new MetadataService.LayoutSection();
754+
newLayoutSection.style = 'OneColumn';
755+
MetadataService.LayoutColumn newLayoutColumn = new MetadataService.LayoutColumn();
756+
MetadataService.LayoutItem newLayoutItem = new MetadataService.LayoutItem();
757+
newLayoutItem.field = 'TestField__c';
758+
newLayoutColumn.layoutItems = new List<MetadataService.LayoutItem> { newLayoutItem };
759+
newLayoutSection.layoutColumns = new List<MetadataService.LayoutColumn> { newLayoutColumn };
760+
layout.layoutSections.add(newLayoutSection);
761+
762+
// Update the Layout
763+
handleSaveResults(
764+
service.updateMetadata(
765+
new MetadataService.Metadata[] { layout })[0]);
766+
}
767+
740768
public static void updatePicklist()
741769
{
742770
MetadataService.MetadataPort service = createService();
@@ -886,4 +914,4 @@ public with sharing class MetadataServiceExamples
886914
if(messages.size()>0)
887915
throw new MetadataServiceExamplesException(String.join(messages, ' '));
888916
}
889-
}
917+
}

0 commit comments

Comments
 (0)