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
bohemianguy100bohemianguy100 

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:selectList>      
				</apex:pageblocksectionItem>                                                  
				<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!"/>             
						</div>             
					</apex:outputpanel>          
				</apex:outputpanel>               
			</apex:pageblocksection>
		</apex:pageblock>
	</apex:form>
</apex:page>

 

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.

sfdcfoxsfdcfox

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).

bohemianguy100bohemianguy100

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?

sfdcfoxsfdcfox

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.

nagalakshminagalakshmi
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

via

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:met="http://soap.sforce.com/2006/04/metadata">
   <soapenv:Header>
      <met:CallOptions>
         <met:client>---</met:client>
      </met:CallOptions>
      <met:SessionHeader>
         <met:sessionId>---</met:sessionId>
      </met:SessionHeader>
   </soapenv:Header>
   <soapenv:Body>
      <update xmlns="http://soap.sforce.com/2006/04/metadata">
     <!--Zero or more repetitions:-->
      <UpdateMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="customfield">
		    <currentName>lead.number__c</currentName>
		                
		      <metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CustomField">
		      <!--Optional:-->
		       <fullName>lead.number__c</fullName>
		       <label>number</label>
		    		<picklist>
		              <picklistValues>
		         			<fullName>new</fullName>
		              </picklistValues>
		         		 <sorted>false</sorted>
		  		</picklist>
		         <type>Picklist</type>
		    </metadata>
         </UpdateMetadata>
       </update>
   </soapenv:Body>
</soapenv:Envelope>
MayankDkPantMayankDkPant

Hi,

 

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: https://salesforce.stackexchange.com/questions/162390/how-to-update-picklist-values-along-with-record-types-using-metadataservice