You need to sign in to do that
Don't have an account?
Derhyk Doggett -
Updating Picklist Through Metadata API Removes Previous Picklist Values
I've used the picklist example from this link as a starting point:
https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataServiceExamples.cls
I've run execute anonymous passing in the parameters. The picklist gets updated with the new value, but marks the previous values as inactive.
Any ideas?
Snippet of code running the update:
webService static void updatePicklistField(String picklistToUpdate, String picklistLabel, String newPicklistValue) { MetadataService.MetadataPort service = createService(); MetadataService.CustomField customField = new MetadataService.CustomField(); customField.fullName = picklistToUpdate; //This is the picklist field. This must in the format Object.Field (append __c for custom object/field) customField.label = picklistLabel; //This field is needed in update customField.type_x = 'Picklist'; metadataservice.Picklist pt = new metadataservice.Picklist(); pt.sorted= false; metadataservice.PicklistValue newPlValue = new metadataservice.PicklistValue(); newPlValue.fullName = newPicklistValue; //This is the new picklist value newPlValue.default_x =false ; pt.picklistValues = new list<metadataservice.PicklistValue>{newPlValue}; customField.picklist = pt ; List<MetadataService.SaveResult> results = service.updateMetadata( new MetadataService.Metadata[] { customField }); handleSaveResults(results[0]); }
Run Execute Anonymous below:
MetadataHandler.updatePicklistField('Account.Access_Type__c','Access Type','Metadata');
Results in the following. note the now one active value and newly marked inactive values:
Okay figured this out. If anyone figures this out without having to include the whole active list of values, please comment. The solution below will leave a recent system timestamp on all active values when new ones are inserted.
In a nutshell, use describe to get the Object, then Object Field Map, use that map to get the field to update, describe that field, get the Picklist values. If the picklist values are active, include them in the list to update.
--Derhyk
All Answers
Okay figured this out. If anyone figures this out without having to include the whole active list of values, please comment. The solution below will leave a recent system timestamp on all active values when new ones are inserted.
In a nutshell, use describe to get the Object, then Object Field Map, use that map to get the field to update, describe that field, get the Picklist values. If the picklist values are active, include them in the list to update.
--Derhyk