You need to sign in to do that
Don't have an account?

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.
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.
check:
http://www.forcetree.com/2009/06/dynamically-add-values-to-picklist.html
yes you can , by creating SOAP envelop
and i have successfully create soap envelope to add new picklist value in picklist field
via
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