+ Start a Discussion
DivyanshD SinghDivyanshD Singh 

Unable to deploy list of Metadata to Page Layout by apex

When i deploy Layout with 1 field everything works fine whereas when i deploy fields as a list , not even a single field is showing in the Layout. Below is the code i am using for field creation, then assign profile and atlast add it to the Layout:
public class DynamicDatapoints {
    public static void dynamicCreation(list<String> lstOfDataPointsToInsert){
        
		//create instance and session id for metadata service
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
		
		//create instance for metadata create custom field class
       
        List<MetadataService.CustomField> lstAllCustoField = new List<MetadataService.CustomField>();
        for(integer i=0; i<lstOfDataPointsToInsert.size(); i++){
             MetadataService.CustomField customField = new MetadataService.CustomField();
			customField.fullName = lstOfDataPointsToInsert[i];
			customField.label = lstOfDataPointsToInsert[i];
			customField.type_x = 'Text';
			customField.length = 42;
            lstAllCustoField.add(customField);
            List<MetadataService.SaveResult> results =
            service.createMetadata(new List<MetadataService.Metadata> { customField });
			system.debug('results>>'+results);
        }          
		
		//create instance for metadata profile class
        MetadataService.Profile admin = new MetadataService.Profile();
        for(String str: lstOfDataPointsToInsert){
			admin.fullName = 'Admin';
			admin.custom = false;
			MetadataService.ProfileFieldLevelSecurity fieldSec = new MetadataService.ProfileFieldLevelSecurity();
			fieldSec.field=str;
			fieldSec.editable=true;
			admin.fieldPermissions  = new MetadataService.ProfileFieldLevelSecurity[] {fieldSec} ;
			List<MetadataService.SaveResult> result = service.updateMetadata( new MetadataService.Metadata[] { admin });
			system.debug('Success FLS' + result);
		}     

        //Adding created fields to a Layout
         Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
        List<Metadata.Metadata> layoutList  = Metadata.Operations.retrieve(Metadata.MetadataType.Layout, new List<String> {'Account-Account Layout'});
        if(layoutList != null && !layoutList.isEmpty()){
			Metadata.Layout layoutMetadata = (Metadata.Layout) layoutList.get(0);
			list<Metadata.Layout> listlayoutMetadata = new list<Metadata.Layout>();
			
			Metadata.LayoutSection sec = new Metadata.LayoutSection();
			sec.customLabel = true;
			sec.detailHeading = true;
			sec.editHeading = true;
			sec.label = 'Keyword fields';
			sec.style = metadata.LayoutSectionStyle.OneColumn;
			system.debug('inside Section');
			Metadata.LayoutColumn LayoutColumnsItems = new Metadata.LayoutColumn();
            list<Metadata.LayoutColumn> listLayoutColumnsItems = new list<Metadata.LayoutColumn>();
            
			Metadata.LayoutItem layoutField =new Metadata.LayoutItem();	 
	   //     list<Metadata.LayoutItem> listlayoutField =new list<Metadata.LayoutItem>();
			Metadata.DeployContainer dc;
			list<Metadata.DeployContainer> listdc =new list<Metadata.DeployContainer>();  
			String field;
			
			for(String st: lstOfDataPointsToInsert){
				
				field = st.removeStart('Account.'); 
				//layoutField = new Metadata.LayoutItem();				
				layoutField.field = field;
				layoutField.behavior = metadata.UiBehavior.Edit;
				system.debug('inside layout section' + field);
				//LayoutColumnsItems = new Metadata.LayoutColumn();
				LayoutColumnsItems.layoutItems.add(layoutField);
                
                system.debug('check Items' + LayoutColumnsItems);
                sec.layoutColumns.add(LayoutColumnsItems);
                layoutMetadata.layoutSections.add(sec);
                //listLayoutColumnsItems.add(LayoutColumnsItems);
				
				
				listlayoutMetadata.add(layoutMetadata);
			}
			
            system.debug('check sectionss -->' + listlayoutMetadata);
            for(Metadata.Layout objLaoutMeta : listlayoutMetadata){
                dc = new Metadata.DeployContainer();
                dc.addMetadata(objLaoutMeta);
                system.debug('dc>>'+dc);
                Id jobid2 = Metadata.Operations.enqueueDeployment(dc,null); 
                system.debug('Metadata Deployment'+jobid2);
            }