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
Parteek Goyal 3Parteek Goyal 3 

How to put value in custom picklist

Hi All,

I want to use custom picklist on vf page and i want to put five account name in this custom picklist. Is it possible?
Please help me...
Best Answer chosen by Parteek Goyal 3
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Prateek,

Here is the code for static picklist values over vf page . You can use properties and actions over page and controller to work with it.
<apex:page>
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection >
                    <apex:selectList size="1">
                         <apex:selectOption itemValue="Account 1" itemLabel="Account 1"/>
                         <apex:selectOption itemValue="Account 2" itemLabel="Account 2"/>
                         <apex:selectOption itemValue="Account 3" itemLabel="Account 3"/>
                         <apex:selectOption itemValue="Account 4" itemLabel="Account 4"/>
                         <apex:selectOption itemValue="Account 5" itemLabel="Account 5"/>
                    </apex:selectList>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Here is the code for dynamically populating picklist values from controller.
<!-- Page: -->
<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!countries}" multiselect="true">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><p/>

        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>

    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel>
                    <p>You have selected:</p>
                    <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>
            
/*** Controller: ***/
    public class sampleCon {
        String[] countries = new String[]{};
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }
            
        public String[] getCountries() {
            return countries;
        }
            
        public void setCountries(String[] countries) {
            this.countries = countries;
        }
    }

All Answers

kavya akavya a
​<apex:page controller="customPickListInVFDemoController" tabStyle="Account">
  <apex:form >
<apex:pageBlock title="Custom PickList Demo" id="out">
<apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="out" status="actStatusId"/>
            <apex:actionStatus id="actStatusId">
                <apex:facet name="start">
                    <img src="/img/loading.gif" />
                </apex:facet>
            </apex:actionStatus>
        </apex:pageBlockButtons>

 <apex:pageBlockSection title="Custom Picklist Using selectList and selectOption" collapsible="false">
            <apex:selectList value="{!selectedCountry1}" multiselect="false" size="1">
                <apex:selectOption itemValue="INDIA" itemLabel="India"/>
                <apex:selectOption itemValue="USA" itemLabel="USA"/>
                <apex:selectOption itemValue="United Kingdom" itemLabel="UK"/>
            </apex:selectList>
             
   
        </apex:pageBlockSection>

</apex:pageblock>
  </apex:form>
</apex:page>

Custom picklist
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Prateek,

Here is the code for static picklist values over vf page . You can use properties and actions over page and controller to work with it.
<apex:page>
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection >
                    <apex:selectList size="1">
                         <apex:selectOption itemValue="Account 1" itemLabel="Account 1"/>
                         <apex:selectOption itemValue="Account 2" itemLabel="Account 2"/>
                         <apex:selectOption itemValue="Account 3" itemLabel="Account 3"/>
                         <apex:selectOption itemValue="Account 4" itemLabel="Account 4"/>
                         <apex:selectOption itemValue="Account 5" itemLabel="Account 5"/>
                    </apex:selectList>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Here is the code for dynamically populating picklist values from controller.
<!-- Page: -->
<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!countries}" multiselect="true">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><p/>

        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>

    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel>
                    <p>You have selected:</p>
                    <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList>
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>
            
/*** Controller: ***/
    public class sampleCon {
        String[] countries = new String[]{};
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }
            
        public String[] getCountries() {
            return countries;
        }
            
        public void setCountries(String[] countries) {
            this.countries = countries;
        }
    }
This was selected as the best answer
bob_buzzardbob_buzzard
If you want to put accounts into a picklist, you have to generate the SelectOption options dynamically in the controller.  You haven't said how the accounts will be identified, so the example below uses the first 5 returned by a query.

Controller snippet :
 
public String selectedAcc {get; set;}

public List<SelectOption> accOtps()
{
   List<SelectOption> results=new List<SelectOption>();
   for (Account acc : [select id, Name from Account LIMIT 5])
   {
      results.add(new SelectOption(acc.Name, acc.Name);
   }

   return results;
}

Page snipppet:
 
<apex:selectList value="{!selectedAcc}" size="1">
   <apex:selectoptions value="{!accOpts}" />
</apex:selectList>

This generates a select where the option's label and value are both the account name.  When the user selects a value and submits the form, selectedAcc will contain the name of the account chosen. If you want the id of the selected account, simply change the SelectOption creation to:
 
results.add(new SelectOption(acc.id, acc.Name);

and change the type of selectedAcc to Id.