2

Unable to update manage package picklist field entries using apex code (metadata API).

I tried using two ways of creating picklist values of metadata API.

Way 1:

    public static void updatePicklistField() {
      MDService.MetadataPort service = createService();
      MDService.CustomField customField = new MDService.CustomField();
      customField.fullName = 'namespacePrefix__ObjectName__c.namespacePrefix__PickList_Field__c';
      customField.label = 'Picklist Field';
      customField.type_x = 'Picklist';
      MDService.ValueSet valSet = new MDService.ValueSet();
      valSet.controllingField = 'namespacePrefix__PickList_Field__c';
      valSet.restricted = false;
      MDService.ValueSetValuesDefinition valueDef = new MDService.ValueSetValuesDefinition();
      valueDef.sorted = false;
      MDService.CustomValue value = new MDService.CustomValue();
      value.fullName = 'New Value';
      value.default_x = false ;
      value.isActive = true;
      valueDef.value = new List<MDService.CustomValue> {value};

      valSet.valueSetDefinition = valueDef;
      customField.valueSet = valSet ;
      List<MDService.SaveResult> results = service.updateMetadata(new MDService.
Metadata[] { customField });
      handleSaveResults(results);
    }

Way 2:

  public static void createPicklistEntries(String requestContext) {
    MDService.MetadataPort service = createService();
    MDService.CustomField customField = new MDService.CustomField();
    customField.fullName = 'namespacePrefix__ObjectName__c.namespacePrefix__PickList_Field__c';
    customField.label = 'Picklist Field';
    customField.type_x = 'Picklist';
    MDService.Picklist pt = new MDService.Picklist();
    pt.sorted = false;
    MDService.PicklistValue value = new MDService.PicklistValue();
    value.fullName = 'New Value';
    value.default_x = false ;
    pt.picklistValues = new List<MDService.PicklistValue>{value};
    customField.picklist = pt ;
    customFieldList.add(customField);
    List<MDService.SaveResult> results = service.updateMetadata(new MDService.Metadata[] { customField });
    handleSaveResults(results);
}

Can someone suggest some other or better way to do this?

I don't want to do this manually (adding new picklist values to picklist field).

Getting below error on execution of above code:

MetadataService.MetadataServiceException: Cannot modify managed object: entity=CustomFieldDefinition, component=00N7xxxxxxxxxxxh, field=PicklistOptionsFlagRestrictedPicklist, state=installed (CANNOT_MODIFY_MANAGED_OBJECT).

  • are you getting any erros or just not working? – Pranay Jaiswal Mar 7 at 10:41
  • Are you using post install script or running this code on click of some button? – Manjot Singh Mar 8 at 7:31
  • @ManjotSingh tried both ways of doing, running in post-install class & on some button click after an upgrade. – Ashwani Kumar Mar 11 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.