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 values apex


I need to create a visualforce page to allow some users without admin rights to add values to a picklist.
I have this VF code :
<apex:page controller="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="Picklist" for="values" />
                    <apex:selectList value="{!test}" size="1" id="values">
                        <apex:actionSupport event="onchange" reRender="newvalue" />
                        <apex:selectOptions value="{!picklistnames}"/>
                <apex:outputpanel id="newvalue">
                        <div style="position:relative;left:75px;">             
                            <apex:outputlabel value="New value" for="newval" />
                            <apex:inputText value="{!newval}" id="newval"/>
                            <apex:commandbutton action="{!saverec}" value="Add!"/>

And this APEX code :
public class dynamicpicklist
    public List<SelectOption> statusOptions { get;set; }
    public String test{get; set;}
    public List<SelectOption> getpicklistnames()
        Schema.DescribeFieldResult statusFieldDescription = Case.Test__c.getDescribe();
        statusOptions = new list<SelectOption>();
        for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel(), false));
        return statusOptions;
    public String newval{get; set;}
    public void saverec()
        Case newrec = new Case(Test__c=newval);
        insert newrec;

The value is added in the picklist but it is inactive. Can you help me to set the new value active by default ?
Thanks for the help.
Akhil TandonAkhil Tandon

Your meathod (saverec) is inserting new values into a picklist field for a specific record (Case newrec). It is not making any meta data changes. You would have to consume metadata API to make updates to fields.

Akhil Tandon