2

I am trying to create and update picklist and adding its value dynamically using metadata service api. But I got to know that MetadataService.CustomField.picklist is deprecated and using valueSet instead.

    MetadataService.ValueSet picklistValueSet = new MetadataService.ValueSet();

    //For each ValueSet, we have either ValueSetValuesDefinition or ValueSettings and some other attributes
    MetadataService.ValueSetValuesDefinition valueDefinition = new MetadataService.ValueSetValuesDefinition();

    List<MetadataService.CustomValue> values = new List<MetadataService.CustomValue>();
    MetadataService.CustomValue customValue1 = new MetadataService.CustomValue();

        customValue1.fullName = PickfullName ; // dynamically coming 
        customValue1.description = Pickdescription;
        customValue1.isActive = PickisActive;
        customValue1.default_x = Pickdefault;
        customValue1.label = Picklabel;
        values.add(customValue1);

    //It will be list of CustomValue
    //valueDefinition.value = values;
    //valueDefinition.sorted = false;

for(integer k=0; k<values.size();k++){
          System.debug('*******valueDefinition.value.add(values[k])*******'+valueDefinition.value.add(values[k]));
       valueDefinition.value.add(values[k]);

       valueDefinition.sorted = false; 
    } 
    //set the valueSetDefinition
    picklistValueSet.valueSetDefinition = valueDefinition;

    //Set the valueSet for picklist type
    customField.valueSet = picklistValueSet;
    //customObj.fields.add(customField); //  ---> doubt on this line -- seems like I need to add it into CustomFields[] 
    System.debug('********customField*****'+customField); // it shows only one value that's why saves with one value
    System.debug('********customField.valueSet*****'+customField.valueSet);    
    List<MetadataService.SaveResult> results = service.createMetadata(new MetadataService.Metadata[] { customField });              
    handleSaveResults(results[0]); 

:Referred this old thread

But not working.

  • 2
    Best you post a copy of the code what isn't working and an exact copy of the error message (if any). – Keith C Jan 12 at 10:24
  • I am able to create the picklist and able to add the value in the picklist though metadata API but now the issue is, only one value getting inserted in the picklist instead of all. I am updating the question with my code. – Ashutosh Jan 12 at 13:13
  • It would seem that you need to iterate on the list values when you set valueDefinition.value = values; It appears that you're only setting values[0]. – crmprogdev Jan 12 at 13:48
  • I tried to iterate over the "values" using loop but still not working. I have updated the code in the question. – Ashutosh Jan 14 at 8:44

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.