1

I have a table & in it there is an attribute that is a picklist with 4 values, as seen:

img1

I'm trying to make a dropdown selectlist, something that would look like this:

img2

but adding these options manually by hand is obviously tedious, especially because I am going to be adding more values to the picklist all the time, and would require the dropdown menu to keep up. How do I make a dropdown menu like this, that adds options based on values from a picklist? I already have a basic visaulforce page that uses the standard automatically generated controller, and I have no other classes made.

This is my visualforce page:

<apex:page docType="html-5.0" standardController="Student__c">

<apex:form >
    <apex:pageBlock >

        <!-- SECTION ONE -->
        <apex:pageBlockSection title="One">
            <apex:inputField value="{!Student__c.aaa__c}" />
            <apex:inputField value="{!Student__c.bbb__c}" />
        </apex:pageBlockSection>

        <!-- SECTION TWO -->
        <apex:pageBlockSection title="Two">
            <!-- here is where my dropdown menu should come -->
        </apex:pageBlockSection>

        <!-- SAVE BUTTON -->
        <apex:commandButton value="Save" action="{!save}" rerender="error"/>

    </apex:pageBlock>
</apex:form>

1
  • 2
    If the field is a pick list then using an input field as you have will always show the fields available in the pick list.
    – Eric
    Sep 5, 2017 at 0:58

1 Answer 1

5

The easiest way, if it works for your use case is to bind directly to this field and let the VF tag build the select list for you.

<apex:inputField value="{!Student__c.YourFieldName__c}" />

If you need to build it manually you can create a method in a custom controller extension class which returns options and then bind to it for the options list - storing the selected item in a property or an attribute on the controller extension too.

public List<SelectOption> getMySelectOptions() {

    // do a field describe of the picklist field
    Schema.DescribeFieldResult describeResult = Student__c.YourFieldName__c.getDescribe();

    // get the picklist values from this describe result
    List<Schema.PicklistEntry> entries = describeResult.getPicklistValues();

    // list of options to be returned to the VF page
    List<SelectOption> optionsToReturn = new List<SelectOption>();

    for (Schema.PicklistEntry pEntry : entries) {
        if (pEntry.isActive()) {
            optionsToReturn.add(new SelectOption(pEntry.getValue(), pEntry.getLabel()));
        }
    }

    return optionsToReturn;
}
1
  • I was able to bind directly to this field, thank you!
    – amko
    Sep 5, 2017 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .