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

add picklist value metadata

Is it possible to add picklist values (the metadata) using apex and a VF page?


I have a VF page that displays the lead source picklist values from the contact object on a VF page:


public List<SelectOption> getLeadSourceNames()
  		List<SelectOption> options = new List<SelectOption>();
   		Schema.DescribeFieldResult fieldResult = Contact.LeadSource.getDescribe();
   		List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
   		for( Schema.PicklistEntry f : ple)
      		options.add(new SelectOption(f.getLabel(), f.getValue()));
   		return options;


On my VF page, I display the selectList and if the user select 'Other' it will open a text field where they can enter a new value and save.  It saves the value to the record, but doesn't add the new value to the actual picklist value metadata.  Is there a ay to do this?


Here is my VF page:


<apex:page standardController="Contact" extensions="DynamicPicklist" sidebar="false" >
	<apex:form >
		<apex:sectionHeader title="Dynamic Picklist" subtitle="Reusable code"/>
		<apex:pageblock >
			<apex:pageBlockSection title="Dynamic picklist" columns="1">      
				<apex:pageblocksectionItem >          
					<apex:outputlabel value="Lead Source" for="values" />          
					<apex:selectList value="{!leadSource}" size="1" id="values">              
						<apex:actionSupport event="onchange" reRender="newvalue" />              
						<apex:selectOptions value="{!leadSourceNames}"/>          
				<apex:outputpanel id="newvalue">             
					<apex:outputpanel rendered="{!leadSource == 'Other'}">             
						<div style="position:relative;left:75px;">                               
							<apex:outputlabel value="New value" for="newval" />                  
							<apex:inputText value="{!newpicklistvalue}" id="newval"/>                  
							<apex:commandbutton action="{!saverec}" value="Add!"/>             


Here is the saverec method:


public void saverec(){   
	con.LeadSource = newpicklistvalue;
	update con; 	


I would like to have the ability to add new picklist values from the VF page.  Is it possible using the metadata API?  If so, how?  I can't find any sample code that explains how to accomplish this.


Thanks for any help.


Technically... no. You'd have to make a call to the metadata API, which you can't do directly through Apex Code (you'd have to callout back to the server using SOAP).


Thanks for the response.  If I understand correctly, I would need to make a callout to an external webservice from my apex class method and the webservice would then access the metadata API and update the picklist with the new value?  Is that correct? 


Any code samples of how to accomplish that?


That's correct. The metadata API (the API that actually allows you to modify metadata, unlike the Web Services API, which is read-only), must be called through a callout. Even worse, the API isn't guaranteed to be synchronous, even for a single field modification, so it would be non-trivial to check for a success status. A possibly better alternative might be a custom object, using a lookup field; it's a lot easier to insert into a custom object than using metadata.

Hi, I am also having same requirement. Can you please share the code if it is solved.... Please help me out.... Thanks, Lakshmi
Apex LearnerApex Learner

yes you can , by creating SOAP envelop 


and i have successfully create soap envelope to add new picklist value in picklist field


<soapenv:Envelope xmlns:soapenv="" xmlns:met="">
      <update xmlns="">
     <!--Zero or more repetitions:-->
      <UpdateMetadata xmlns:xsi="" xsi:type="customfield">
		      <metadata xmlns:xsi="" xsi:type="CustomField">



Can any one tell me how to create SOAP Envelope and how to call it. Does it require any external service /application?


Please elaborate.


Any help will be appreciated.

Thanks in advance.


With Regards,

Mayank Pant


Brian E MillerBrian E Miller
You can now use MetadataService to adjust picklist values from Apex code.  See an example here: