function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashritha SAshritha S 

Problem while creating page layout using metadata api

Hi ,

I tried creating page layout using metadata api using the code
  MetadataService.MetadataPort service = new MetadataService.MetadataPort();  
  service.SessionHeader = new MetadataService.SessionHeader_element();
  service.SessionHeader.sessionId = UserInfo.getSessionId();

    MetadataService.Layout layout = new MetadataService.Layout();
    layout.fullName = 'Case-My Layout';
    layout.layoutSections = new List<MetadataService.LayoutSection>();
    MetadataService.LayoutSection layoutSection = new MetadataService.LayoutSection();
    layoutSection.editHeading = true;
    layoutSection.label = 'Description Information';
    layoutSection.style = 'TwoColumnsTopToBottom';
    layoutSection.layoutColumns = new List<MetadataService.LayoutColumn>();
    MetadataService.LayoutColumn layoutColumn = new MetadataService.LayoutColumn();
    layoutColumn.layoutItems = new List<MetadataService.LayoutItem>();
   MetadataService.LayoutItem layoutItem1 = new MetadataService.LayoutItem();
      layoutItem1.behavior = 'Required';
      layoutItem1.field = 'Contact';
      layoutColumn.layoutItems.add(layoutItem1);
   layoutSection.layoutColumns.add(layoutColumn);
   layout.layoutSections.add(layoutSection);
   List<MetadataService.SaveResult> results =     
        service.createMetadata(
        new MetadataService.Metadata[] { layout });    
  System.debug(results[0].errors[0].message);
 
I get the error message as USER_DEBUG [106]|DEBUG|Layout must contain an item for required layout field: ContactId  .
I would be grateful if someone could help me solve this issue.

Thanks
NaveenReddyNaveenReddy
Contact Name is marked as required by default in salesforce for Case object.
While creating and saving a layout must include field Contact Name in that layout.

Regards,
Naveen