5

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();
}
5
  • 1
    Are you trying to update a standard picklist field?
    – Oxene
    Commented Jan 20, 2014 at 7:26
  • 2
    question may be easier to understand if you include some of your specific code Commented Jan 20, 2014 at 7:26
  • In the UI, you can't create or edit custom fields directly on the Tasks object - there's an Activity branch in the setup tree, and you create "Activity Custom Fields" which then get created on Task, Event and Open Activity. I don't know how this plays out in the metadata API - whether there's a different object that you need to create the fields on for them to propagate to the other object or something. Commented Jan 20, 2014 at 16:53
  • @Thomas Taylor Well, it may be something about how the activity is special. You can query and update tasks with the SOAP API, so I think it's generally correct. Also, I routinely update task along with many other objects via the metadata API using XML deployments. I did try changing the object to Activity but that doesn't work at all (I think it was a message saying that the object is not accessible or something along those lines).
    – donmac23
    Commented Jan 21, 2014 at 21:32
  • @Sdry have added the key part of the code.
    – donmac23
    Commented Jan 21, 2014 at 21:52

1 Answer 1

1

I was getting this error when trying Task.Customfield but I was able to fix it by using Activity.CustomField. Hope this helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .