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
Bruce GreerBruce Greer 

Using API, how to add new value to picklist for standard field without changing existing values.

How do I use the Metadata API to add a new picklist value to a standard field that already has many custom values.  Specifically, I do not want to overwrite the picklist because that would delete the custom values.  I just want to add a single new value to the list.
SandhyaSandhya (Salesforce Developers) 
Hi Bruce Greer,

Please refer below sample code.
 
In following code we add two and three as a picklist value Picklist__c field on Lead.
 
Use the following code in your APEX class:
 
public static void UpdatePicklistField()
	{
	    MetadataService.MetadataPort service = createService();     
	    MetadataService.CustomField customField = new MetadataService.CustomField();
	    customField.fullName = 'Lead.picklist__c';
	    customField.label = 'picklist';
	    customField.type_x = 'Picklist';
	    metadataservice.Picklist pt = new metadataservice.Picklist();
	    pt.sorted= false;
	    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 ;
	    pt.picklistValues = new list<metadataservice.PicklistValue>{two,three};
	    customField.picklist = pt ;
	    MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
	    ut.currentName='Lead.picklist__c';
	    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;		
	}

Please refer below post for similar discussion.

https://help.salesforce.com/apex/HTViewSolution?id=000002778&language=en_US
 
http://salesforce.stackexchange.com/questions/49057/adding-values-to-a-picklist-using-apex
 
https://developer.salesforce.com/forums?id=906F000000092beIAA
 
Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya


 
Bruce GreerBruce Greer
Sandhya,

Thank you for your quick response.  However, I don't think this is going to work for me.  Your code appears to *set* the picklist on a *custom* field.  My requirements are that I need to *add* a new value to an existing picklist on a *standard* field.  Specifically, I need to add a new value to the LeadSource picklist field on both the Lead and Contact standard objects.

Thanks!
-Bruce