I am using the metadata API from Apex code thanks to Andrew Fawcett's very helpful code to do this. I have a some Apex code that goes and updates a series of multiselect picklists with the same set of values. This works well on 6 out of the 7 picklists I need to update but I've run into a problem with updating one on the task object. The same code is used for all of them, so it appears it is something specific about tasks. I am calling "update" method to make the change and then checkstatus to retrieve the results (via the metadataservice.cls file). I've tried creating a new picklist with only a single value and then updating just that value. Once again, it works on other objects (including account and various custom objects) but fails with the same message on task. The error reported is "INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST: Entity Enumeration Or ID: bad value for restricted picklist field: Task". Many thanks in advance for any assistance in solving this.
Here's the key part of the code that takes a list of custom fields (plFields) that have already been verified as having a picklist and goes and assigns the new picklist values (plValues) to each one. As I mentioned, this works for updating 6 out of 7 of the picklists. They are all custom fields.
public Status UpdateLists() {
List<MetadataService.UpdateMetadata> updates = new List<MetadataService.UpdateMetadata>();
// Update the picklists to match the names of the product lines
for (String fld : plFields) {
Schema.DescribeFieldResult fr = mapFldDetails.get(fld).getDescribe();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = fld;
customField.label =fr.getLabel();
customField.type_x = fr.getType() == Schema.DisplayType.Picklist ? 'Picklist': 'MultiselectPicklist';
if (fr.getType() == Schema.DisplayType.MultiPicklist) {
// this value not available from internal Schema.DescribeFieldResult class, so we either have a single value or keep a record against each picklist here in the code
customField.visibleLines = 4;
}
metadataservice.Picklist pt = new metadataservice.Picklist();
pt.sorted= sorted;
pt.picklistValues = new List<metadataservice.PicklistValue>();
for (String val : plValues) {
metadataservice.PicklistValue pv = new metadataservice.PicklistValue();
pv.fullName = val;
pv.default_x = false;
pt.picklistValues.add(pv);
}
customField.picklist = pt;
MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
ut.currentName= fld;
ut.metadata= customField;
updates.add(ut);
}
results = createService().updateMetadata(updates);
return GetDisplayStatus();
}