-2

Can anyone suggest me how to add new value to the existing custom picklist using apex?

1
  1. Running user needs to have Customize Application privilege so you really want this to be an admin type user.

  2. Read the examples in the GIT package carefully - there is one for picklists. You need to read the CustomField from the MetaData API first then update, otherwise you'll smash all the existing picklist entries.

The relevant example in the package's MetadataServiceExamples.cls file is here:

public static void updatePicklist()
{
    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]);        
}

Refered from this link : here

  • I know the package you're referring to, but others may not. Can you include an explicit link/description? – David Reed Oct 10 '19 at 14:54
  • Thanks for your quick response. I'm getting error "variable doesn't exist: picklist". customField.picklist.picklistValues.add(two); customField.picklist.picklistValues.add(three); – Rkumar Oct 10 '19 at 14:54
  • done @DavidReed – Mohit Kulkarni Oct 10 '19 at 14:58
  • please upvote & select as best answer if u find it useful @Rkumar – Mohit Kulkarni Oct 10 '19 at 14:59
  • I'm getting error "variable doesn't exist: picklist". customField.picklist.picklistValues.add(two); customField.picklist.picklistValues.add(three); – Rkumar Oct 10 '19 at 15:05

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