You need to sign in to do that
Don't have an account?
DivyanshD Singh
add fields to same layout section by metadata API
I am trying to add new field items to a Layout section.Everytime a new section is getting created even if i am assigning the same Section.
If i deploy the same section to the Layout, it gives an error 'Field appears more than once' as it tries to deploy the same fields again.
If i make a new column it creates a new section automatically.
How can i add new LayoutColumn to same section through Metadata API.
Below is my code :
If i deploy the same section to the Layout, it gives an error 'Field appears more than once' as it tries to deploy the same fields again.
If i make a new column it creates a new section automatically.
How can i add new LayoutColumn to same section through Metadata API.
Below is my code :
public static void addFieldToLayout(String[] lstOfDataPointsToInsert){ list<String> listTrimmedFields = new list<String>(); MetadataService.LayoutColumn newLayoutColumn = new MetadataService.LayoutColumn(); MetadataService.LayoutColumn newLayoutColumn1 = new MetadataService.LayoutColumn(); list<MetadataService.LayoutColumn> mdcolumn1 = new List<MetadataService.LayoutColumn>(); list<MetadataService.LayoutColumn> mdcolumn2 = new List<MetadataService.LayoutColumn>(); list<MetadataService.LayoutColumn> listColumns = new list<MetadataService.LayoutColumn>(); list<MetadataService.LayoutColumn> listColumns1 = new list<MetadataService.LayoutColumn>(); List<MetadataService.LayoutItem> lstLayoutItems = new List<MetadataService.LayoutItem>(); List<MetadataService.LayoutItem> lstLayoutItems1 = new List<MetadataService.LayoutItem>(); List<MetadataService.LayoutItem> lstLayoutItems2 = new List<MetadataService.LayoutItem>(); List<MetadataService.LayoutItem> lstLayoutItems3 = new List<MetadataService.LayoutItem>(); list<String> listString = new list<String>(); list<String> listString1 = new list<String>(); MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); // Read the Layout MetadataService.Layout layout = (MetadataService.Layout) service.readMetadata('Layout', new String[] { 'IBP__Ifbyphone_Call__c-IBP__Ifbyphone_Call_Layout' }).getRecords()[0]; list<MetadataService.Layout> listLayout = new list<MetadataService.Layout>(); for(Integer i=0; i<lstOfDataPointsToInsert.size(); i++){ if(lstOfDataPointsToInsert.size() > 5){ listTrimmedFields.add(lstOfDataPointsToInsert.remove(i)); } } system.debug('actaul list-->' + lstOfDataPointsToInsert + 'Trimmmmed List -->' + listTrimmedFields); // Add Layout section, layout, item and field if(layout.layoutSections==null ) layout.layoutSections = new List<MetadataService.LayoutSection>(); MetadataService.LayoutSection newLayoutSection = new MetadataService.LayoutSection(); if(sections() == null || sections().label != 'Keyword fields'){ system.debug('inside thissssssss'); newLayoutSection.customLabel = true; newLayoutSection.detailHeading = true; newLayoutSection.editHeading = true; newLayoutSection.label = 'Keyword fields'; newLayoutSection.style = 'TwoColumnsLeftToRight'; system.debug('listTrimmedFields>>>1322'+listTrimmedFields); system.debug('lstOfDataPointsToInsert>>>13212'+lstOfDataPointsToInsert); for(String objField : lstOfDataPointsToInsert){ system.debug('ObjectfieldAPIName for 1st 5>>>'+ObjectfieldAPIName(objField, false)); MetadataService.LayoutItem newLayoutItem = new MetadataService.LayoutItem(); newLayoutItem.field = ObjectfieldAPIName(objField, false); //newLayoutItem.behavior = 'Edit'; lstLayoutItems.add(newLayoutItem); system.debug('in 1st columnnnnnn---->' +lstOfDataPointsToInsert); } for(String objField1 : listTrimmedFields){ system.debug('ObjectfieldAPIName for last 5>>>'+ObjectfieldAPIName(objField1, false)); MetadataService.LayoutItem newLayoutItem1 = new MetadataService.LayoutItem(); newLayoutItem1.field = ObjectfieldAPIName(objField1, false); //newLayoutItem.behavior = 'Edit'; lstLayoutItems1.add(newLayoutItem1); system.debug('in 2nd list column'+listTrimmedFields); //newLayoutColumn.layoutItems = new List<MetadataService.LayoutItem> { newLayoutItem }; } system.debug('check for 1st column ' + lstLayoutItems + 'Check for 2nd column ' + lstLayoutItems1); newLayoutColumn.layoutItems = lstLayoutItems; newLayoutColumn1.layoutItems = lstLayoutItems1; system.debug('check newLayoutColumn' + newLayoutColumn + 'Check newLayoutColumn1 List' + newLayoutColumn1); system.debug('newLayoutSection>>>>qw'+newLayoutSection); newLayoutSection.layoutColumns = new List<MetadataService.LayoutColumn> { newLayoutColumn,newLayoutColumn1 }; system.debug('newLayoutSection.layoutColumns>>>>qw'+newLayoutSection.layoutColumns); layout.layoutSections.add(newLayoutSection); } else{system.debug('in elseeeee---->'); system.debug('listTrimmedFields>>>12'+listTrimmedFields); system.debug('lstOfDataPointsToInsert>>>12'+lstOfDataPointsToInsert); for(String objField : lstOfDataPointsToInsert){ system.debug('ObjectfieldAPIName for 3rd 5>>>'+ObjectfieldAPIName(objField, false)); MetadataService.LayoutItem newLayoutItem = new MetadataService.LayoutItem(); newLayoutItem.field = ObjectfieldAPIName(objField, false); //newLayoutItem.behavior = 'Edit'; lstLayoutItems2.add(newLayoutItem); system.debug('in 1st columnnnnnn---->' +lstOfDataPointsToInsert); } for(String objField1 : listTrimmedFields){ system.debug('ObjectfieldAPIName for 4th 5>>>'+ObjectfieldAPIName(objField1, false)); MetadataService.LayoutItem newLayoutItem1 = new MetadataService.LayoutItem(); newLayoutItem1.field = ObjectfieldAPIName(objField1, false); //newLayoutItem.behavior = 'Edit'; lstLayoutItems3.add(newLayoutItem1); system.debug('in 2nd list column'+listTrimmedFields); //newLayoutColumn.layoutItems = new List<MetadataService.LayoutItem> { newLayoutItem }; } system.debug('check for 1st column ' + lstLayoutItems2 + 'Check for 2nd column ' + lstLayoutItems3); MetadataService.LayoutColumn newLayoutColumn12 = new MetadataService.LayoutColumn(); MetadataService.LayoutColumn newLayoutColumn13 = new MetadataService.LayoutColumn(); newLayoutColumn12.layoutItems = lstLayoutItems2; newLayoutColumn13.layoutItems = lstLayoutItems3; system.debug('newLayoutColumn12ncolumn ' + newLayoutColumn12 + 'newLayoutColumn13 2nd column ' + newLayoutColumn13); system.debug('check listtsssssss' + listString + 'Check second List' + listString1); MetadataService.layoutSection tragetSections = sections(); system.debug('tragetSections>>>'+tragetSections.layoutColumns); tragetSections.layoutColumns = new List<MetadataService.LayoutColumn> { newLayoutColumn12,newLayoutColumn13 }; //tragetSections.layoutColumns.add(newLayoutColumn12); //tragetSections.layoutColumns.add(newLayoutColumn13); system.debug('newLayoutSection>>>>!2'+tragetSections); layout.layoutSections.add(tragetSections); system.debug('hereeee'); } handleSaveResults( service.updateMetadata( new metadataService.Metadata[]{layout} )[0]); } public static MetadataService.layoutSection sections(){ MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); MetadataService.Layout layouts = (MetadataService.Layout) service.readMetadata('Layout', new String[] { 'IBP__Ifbyphone_Call__c-IBP__Ifbyphone_Call_Layout' }).getRecords()[0]; MetadataService.LayoutSection mdSection ; for(MetadataService.LayoutSection mdlayout:layouts.layoutSections){ if(mdlayout.label == 'Keyword Fields'){ mdSection = mdlayout; break; } } return mdSection; }