function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Leelakrishna MunirajLeelakrishna Muniraj 

Insert new or update picklist values for existing picklist

consider this picklist field in any object.
Existing picklist having city = xxx
                                               yyy

How should i add new city picklist value zzz then my expected is
 Existing picklist having city = xxx
                                                yyy                                            
                                                zzz


-------------------------------Following Code I Tried----------------------
public class Test_MetadataAPI {
    public static MetadataService.MetadataPort createService(){
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;
    }
public static void UpdatePicklist(){
        MetadataService.MetadataPort service = createService();
    
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'Contact' + '.testPicklist__c';
        customField.label = 'testPicklist';
        customField.type_x = 'Picklist';
        customField.description = '';
        
        // Create the valueSet for picklist type
        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='NewTestOne';// API Name of picklist value
        //customValue1.default = true;//true/false;
        customValue1.description = 'just testing';
        customValue1.isActive = true;//true/false;
        customValue1.label = 'NewTestOne';
        values.add(customValue1);
        
        // it will be list of CustomValue
        valueDefinition.value = values;
        valueDefinition.sorted = false;//false/true;
        
        // Set  the valueSetDefinition
        picklistValueSet.valueSetDefinition = valueDefinition;
        
        // Set the valueSet for picklist type
        customField.valueSet = picklistValueSet;
        //customFields.add(customField);
        
        MetadataService.SaveResult[] results = service.updateMetadata(new List<MetadataService.Metadata> {customField});
    }    
}
 
PriyaPriya (Salesforce Developers) 

Hi Leela,

Kindly refer this similar question and answe it git hub :- 

https://salesforce.stackexchange.com/questions/49057/adding-values-to-a-picklist-using-apex

Please mark it as best answer if it helps.

Regards,

Priya Ranjan