I am writing an apex method where I have to add picklist value dynamically with apex code. I have followed many websites but not able to get this. Please check my code below.

public with sharing class updateLec_OnTimeTablecls
    public static void UpdatePicklistField()
        MetadataService.MetadataPort service = createService();    
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'Scheduler__c.Lectures__c';       // your object. fields name
        customField.label = 'picklist';                 // label of field.
        customField.type_x = 'Picklist';               // type
        metadataservice.Picklist pt = new metadataservice.Picklist();
        pt.sorted= false;
        metadataservice.PicklistValue two = new metadataservice.PicklistValue(); //new value
        two.fullName= 'second';
        two.default_x=false ;
        metadataservice.PicklistValue three = new metadataservice.PicklistValue(); // new value
        three.fullName= 'third';
        three.default_x=false ;
        pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};  
        customField.picklist = pt ;
        MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata(); // here you are updated your value in object.
        ut.metadata= customField;  
        MetadataService.AsyncResult[] results = service.updateMetadata(new List<MetadataService.updateMetadata> {ut});
    public static MetadataService.MetadataPort createService()

        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;    

ERROR:Error: Compile Error: Variable does not exist: picklist at line 20 column 21

If I comment on line 20, I am getting

Error: Compile Error: Invalid type: MetadataService.UpdateMetadata at line 21 column 9

Please help!

  • What is the usecase and why do you want to handle this using Apex? Commented Oct 19, 2020 at 12:12
  • @the_phantom I have to update my picklist field value while I insert a new object record. Let's say, I need to update objects B picklist field value while I insert object A records.
    – SFDC 44
    Commented Oct 19, 2020 at 12:47
  • I am a bit skeptical to answer this as changing metadata from Apex is not a very recommended way. Is this a hard requirement OR there is a chance you can convince your customer to follow a different approach? Commented Oct 19, 2020 at 14:06

1 Answer 1


You're following outdated examples. The Picklist Metadata API type and corresponding field on CustomField were supported until API 37.0.

In later API versions, you must instead use the valueSet property and the ValueSet type.

The fact that you're getting those errors suggests your Metadata API classes were generated from a WSDL that postdates API 37.0, so you'll want to switch to writing your code based on ValueSet.

You must log in to answer this question.

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