Hi,
I have created custom button using apex MetadataService. but it not showing in lightning, it showing only classic mode, how can it fix it?
@Bala BS, You can refer to this: https://v2forcecrm.medium.com/use-of-metadata-api-for-page-layout-customization-v2force-99939cadbdb1
In this given the example of how to add/remove the custom button into the Salesforce mobile app action bar for the layout.
Adding a button into the quick action button we need to create data of MetadataService. PlatformActionListItem data type and add it to the list of platformActionListItem. Like below code snippet.
MetadataService.PlatformActionListItem objPlateformActionListItem = new MetadataService.PlatformActionListItem();
objPlateformActionListItem.actionName ='API_Name_of_Button';
objPlateformActionListItem.actionType = 'CustomButton';
objPlateformActionListItem.sortOrder = 50;
objPlateformActionListItem.subtype = null;
objLayout.platformActionList.platformActionListItems.add(objPlateformActionList Item);
Similarly, we can add the related list like this -
MetadataService.RelatedListItem objRelatedListItem = new MetadataService.RelatedListItem();
objRelatedListItem.relatedList = 'ObjectAPIName c.nameOfRelation c';
objLayout.relatedLists.add(objRelatedListItem);
After all the customization in page layout or say objLayout we need to update this layout in the org. For this purpose, we use updateMetadata(pass List of metadata to be updated); a function of the MetadataService Class. As the function takes layout as a list we have to add objLayout to the List<MetadataService.Metadata> which can be done like this -
//check createService in first code snippet
List<MetadataService.Metadata> lstLayoutsToUpdate = new List<MetadataService.Metadata>(objLayout);
MetadataService.MetadataPort mdpService = createService();
List<MetadataService.SaveResult> lstSaveResults = new List<MetadataService.SaveResult>();
lstSaveResults.addAll(mdpService.updateMetadata(lstLayoutsToUpdate));