Activity Forums Salesforce® Discussions How can we create the Dynamic Picklist?

  • Satyakam

    Member
    August 1, 2016 at 7:14 am

    Hi Mohit,

    Follow the below code:

    Visualforce Page:

    <apex:page controller="picklistController" >
    <apex:form >
    <apex:pageblock >
    <apex:pageBlockSection title="Dynamic picklist">
    <apex:pageblocksectionItem >
    <apex:outputlabel value="City" for="values" />
    <apex:selectList value="{!city}" size="1" id="values">
    <apex:actionSupport event="onchange" reRender="newvalue" />
    <apex:selectOptions value="{!citynames}"/>
    </apex:selectList>
    </apex:pageblocksectionItem>
    <apex:outputpanel id="newvalue">
    <apex:outputpanel rendered="{!city == '--Other--'}">
    <div style="position:relative;left:75px;">
    <apex:outputlabel value="New value" for="newval" />
    <apex:inputText value="{!newCity}" id="newval"/>
    <apex:commandbutton action="{!saveCity}" value="Add!"/>
    </div>
    </apex:outputpanel>
    </apex:outputpanel>
    </apex:pageblocksection>
    </apex:pageblock>
    </apex:form>
    </apex:page>

    Apex Controller:

    public class picklistController
    {
    public String city{get; set;}

    public String newCity{get; set;}

    public List<SelectOption> getcitynames()
    {
    List<SelectOption> options = new List<SelectOption>();
    List<DynamicPicklist__c> citylist = new List<DynamicPicklist__c>();
    citylist = [Select Id, PicklistValue__c FROM DynamicPicklist__c ];
    options.add(new SelectOption('--None--','--None--'));
    for (Integer j=0;j<citylist.size();j++)
    {
    options.add(new SelectOption(citylist[j].PicklistValue__c,citylist[j].PicklistValue__c));
    }
    return options;
    }

    public void saveCity()
    {
    DynamicPicklist__c newrec = new DynamicPicklist__c(PicklistValue__c=newCity);
    insert newrec;
    newCity=NULL;
    }

    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos