Activity Forums Salesforce® Discussions How to make a dynamic picklist in salesforce?

  • Mohit

    Member
    September 29, 2016 at 8:02 am

    Hi Tanu,

    To create a dynamic picklist you can use this code:-

    For visualforce page:-

    <apex:page controller="dynamicpicklistcontroller" action="{!autoRun}">
    <apex:form >
    <apex:pageblock >
    <apex:pageBlockSection id="InfoId" columns="1" >
    <apex:outputLabel value="New Account Status" />
    <apex:selectList size="1" value="{!SelectedValue}">
    <apex:selectOptions value="{!statusOptions}"/>
    </apex:selectList>
    </apex:pageBlockSection>
    </apex:pageblock>
    </apex:form>
    </apex:page>

    For Controller:-

    public class dynamicpicklistcontroller {

    public List<SelectOption> statusOptions { get;set; }
    public List<sObject> listofacc{get;set;}// You can use any sObject for which you want to apply the search
    public void autoRun()
    {
    statusOptions = new list<SelectOption>();
    listofacc = [select name from contact];//you can mention that sobject for which you want to apply query
    for(contact a : listofacc){
    statusOptions.add(new SelectOption(a.name,a.name));
    }
    }

  • Avnish Yadav

    Member
    September 30, 2018 at 12:10 am

    Hello,

    A dynamic picklist allows end users to add values to the list rather than requiring the assistance of an administrator. It can be updated as and when values are required. This is particularly useful if you have processes within your organization whereby you would have to request values to be added to a standard picklist. This could take some time and if you're in the middle of something, it would distract from what you're doing and delay things unnecessarily.

    Thanks.

  • Avnish Yadav

    Member
    September 30, 2018 at 12:12 am

    Here is the code -

    Controller

    public class TestSelectController{
    public List<SelectOption> getYears() {
    List<SelectOption> options = new List<SelectOption>();
    for (Integer i = System.Today().year() - 3; i < System.Today().year() + 3; i++) {
    options.add(new SelectOption(String.valueOf(i), String.valueOf(i)));
    }
    return options;
    }

    public String selectedYear {get; set;}

    public TestSelectController() {
    selectedYear = String.valueOf(System.Today().year());
    }
    }

    Page:

    <apex:page controller="TestSelectController">
    <apex:form>
    <apex:selectList value="{!selectedYear}" size="1">
    <apex:selectOptions value="{!Years}"/>
    </apex:selectList>
    </apex:form>
    </apex:page>

    Thanks.

  • William

    Member
    December 14, 2018 at 4:38 am

    Apex class
    public class ScemaMultiDisplayEx {
    public list<selectOption>notSelectedOption{set;get;}
    public list<selectOption>SelectedOption{set;get;}
    public list<selectOption>SelecteObject {set;get;}
    public set<string>NotSelectedFields{set;get;}
    public set<string>SelectedFields{set;get;}
    public list<string>FieldNames{set;get;}
    Public list<string>nonSelected{set;get;}
    Public list<string>Selected{set;get;}
    public map<string,schema.SObjectType>ObjectMap    {set;get;}
    public map<string,schema.SObjectField>fieldMap {set;get;}
    public list<Sobject>result                     {set;get;}
    public string SelectedObject                   {set;get;}
    public ScemaMultiDisplayEx(){
    ObjectMap= new map<string,schema.SObjectType>();
    notSelectedOption= new list<selectOption>();
    SelectedOption= new list<selectOption>();
    SelecteObject=new list<selectOption>();
    NotSelectedFields= new set<string>();
    SelectedFields = new set<string>();
    Selected= new list<string>();
    nonSelected= new list<string>();
    ObjectMap=schema.getGlobalDescribe();
    fieldMap=new map<string,schema.SObjectField>();
    selectOption op = new selectOption('None','-None-');
    SelecteObject.add(op);
    notSelectedOption.add(op);
    SelectedOption.add(op);
    for(string s:ObjectMap.keySet()){
    selectOption op1= new selectOption(s,s);
    SelecteObject.add(op1);
    }
    }
    public void getOption(){
    schema.SObjectType obj = ObjectMap.get(SelectedObject);
    schema.DescribeSObjectResult res=obj.getDescribe();
    FieldMap=res.Fields.getMap();
    NotSelectedFields.addAll(fieldMap.keyset());
    getValue();
    //system.debug(NotSelectedFields);
    }
    public void getValue(){
    notSelectedOption.clear();
    SelectedOption.clear();
    Selectoption op= new selectoption('None','-None-');
    if(NotSelectedFields.size()>0){
    for(string ss: NotSelectedFields){
    selectoption p1= new selectoption(ss,ss);
    notSelectedOption.add(p1);
    }
    }else{
    notSelectedOption.add(op);
    }
    if(SelectedFields.size()>0){
    for(string s:SelectedFields){
    selectoption p= new selectoption(s,s);
    SelectedOption.add(p);
    }
    }else{
    SelectedOption.add(op);
    }
    }
    public void AddRecords(){
    NotSelectedFields.removeAll(Selected);
    SelectedFields.addAll(Selected);
    getValue();
    }
    public void RemoveRecord(){
    SelectedFields.removeAll(nonSelected);
    NotSelectedFields.addAll(nonSelected);
    getValue();
    }
    }
    Visulforce page
    <apex:page controller="ScemaMultiDisplayEx" showHeader="false" >
    <style>
    .b{
    width:200px;
    height:200px;
    color:Blue;
    }
    </style>
    <apex:form >
    <apex:pageBlock >
    Choose a Object <apex:selectList size="1" value="{!SelectedObject}">
    <apex:selectOptions value="{!SelecteObject}"/>
    <apex:actionSupport event="onchange" action="{!getOption}" reRender="one" />
    </apex:selectList>
    <apex:panelGrid columns="3" id="one">
    <apex:selectList multiselect="true" value="{!Selected}" styleClass="b">
    <apex:selectOptions value="{!notSelectedOption}"/>
    </apex:selectList>
    <apex:panelGrid columns="1">
    <apex:commandButton value="ADD" action="{!AddRecords}" reRender="one" />
    <apex:commandButton value="DEL" action="{!RemoveRecord}" reRender="one" />
    </apex:panelGrid>
    <apex:selectList multiselect="true" value="{!nonSelected}" styleClass="b"  >
    <apex:selectOptions value="{!soption}"/>
    </apex:selectList>
    </apex:panelGrid>
    </apex:pageBlock>
    <apex:pageBlock title="Records" rendered="{! !Isnull(result)}">
    <apex:pageBlockTable value="{!result}" var="a">
    <apex:repeat value="{!FieldNames}" var="b">
    <apex:column value="{!a[b]}" />
    </apex:repeat>
    </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

Log In to reply.

Popular Salesforce Blogs