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 '19 at 10:41
  • Are you using post install script or running this code on click of some button? – Manjot Singh Mar 8 '19 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 '19 at 4:52
0

@ashwani - You are trying to change the field definition which is part of managed package which is not allowed. But Just adding picklist options is allowed, If you are using the Financialforce wrapper class from github, refer to the method in MetadataServiceExample to just update the picklist field with new options. MetadataService.MetadataPort service = createService();

    // Read Custom Field
    MetadataService.CustomField customField =
        (MetadataService.CustomField) service.readMetadata('CustomField',
            new String[] { 'Lead.picklist__c' }).getRecords()[0];

    // Add pick list values
    metadataservice.PicklistValue two = new metadataservice.PicklistValue();
    two.fullName= 'second';
    two.default_x=false;
    metadataservice.PicklistValue three = new metadataservice.PicklistValue();
    three.fullName= 'third';
    three.default_x=false;
    customField.picklist.picklistValues.add(two);
    customField.picklist.picklistValues.add(three);

    // Update Custom Field
    handleSaveResults(
        service.updateMetadata(
            new MetadataService.Metadata[] { customField })[0]);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.