@@ -737,6 +737,34 @@ public with sharing class MetadataServiceExamples
737
737
new MetadataService .Metadata [] { layout })[0 ]);
738
738
}
739
739
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
+
740
768
public static void updatePicklist ()
741
769
{
742
770
MetadataService .MetadataPort service = createService ();
@@ -886,4 +914,4 @@ public with sharing class MetadataServiceExamples
886
914
if (messages .size ()> 0 )
887
915
throw new MetadataServiceExamplesException (String .join (messages , ' ' ));
888
916
}
889
- }
917
+ }
0 commit comments