With the increasing popularity and adoption of the Force.com platform, I am seeing a huge growth in custom Visualforce pages, in particular pages which require interaction with multiple objects. One requirement that comes up regularly is how to display picklist values from an object when the page is using a custom controller. 

In order to achieve this, we have to look at the very powerful and useful Dynamic Apex Describe Information capabilities.  These capabilities allow us to interrogate a field or sObject, and describe their characteristics. In our requirement above, we want to describe the fields on an object, in particular, a picklist field to determine its list of values.

Lets say we have a custom object called OfficeLocation__c. OfficeLocation__c contains a number of fields, one of which is a picklist of country values called, creatively enough, Country__c. Our customer requirements are to include the picklist of countries on a Visualforce page which uses a custom controller. The first thing we need to do, within our controller is use the getDescribe() method to obtain information on the Country__c field:

Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe();

We know that Country__c is a picklist, so we want to retrieve the picklist values:

List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

The only thing left for us to do is map the picklist values into an <apex:selectOptions> tag can use for display. Here is the entire method from our controller to do this:

public List<SelectOption> getCountries()
{
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult =
 OfficeLocation__c.Country__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        
   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
}

With our controller logic all complete, we can call the getCountries() method from our Visualforce page,  and populate the <apex:selectList> tag:

<apex:selectList id="countries" value="{!Office_Location__c.Country__c}"
         size="1" required="true">
  <apex:selectOptions value="{!countries}"/>
</apex:selectList>

tagged , Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • http://developer.force.com Jon Mountjoy

    Hi Quinton
    I’m not sure I understand the premise. Whether you are using a custom controller or not, can’t you simply use the apex:inputfield component to do this?
    Regards,
    Jon

  • http://profile.typekey.com/1220919448s5150/ Jesper Joergensen

    Just to clarify. These describe calls are part of the regular Web Services API. They are not part of the metadata API, although they do return metadata about the objects.

  • A B

    My problem is if I have dependent picklist. I want to fetch the valid picklist values that are based on the other’s picklist value.
    Schema.PicklistEntry does not have a validFor field (in Apex) and/or there is no secondary object that has a map of PicklistEntries and the values that depend on it. Something like the following:
    i.e. (I know these should be greater then and less then symbols and not “[" and "]“):
    Map [String, List[Schema.PicklistEntry]] dependentMap = ((PicklistDescribeObjectType) sObject.field.getDescribe()).getDependencies();
    List[Schema.PicklistEntry] validValues = dependentMap.get(‘NE_CITIES’);
    p.s. “& l t ;”, “& # 6 0 ;”, “& g t ;” and “& # 6 2 ;” do not work reliably in the comments.

  • Varun

    exact same issue as with “A B” above. Has anyone able to use Dependent Picklists in Visualforce pages using APEX classes?

  • http://www.x2od.com David Schach

    There is a dependent picklist example in the Force.com Developer Guide, pp 373-379.

  • Ian Randall

    Awesome – thanks loads – this is exactly what I wanted. The two options I had to provide a picklist on my page were 1) create a dummy Account object, or 2) Hard-code a List in the getter, replicating the values stored in the field… both would have worked and both were pretty urrgghh…
    thanks again :)

  • SIMHA

    Yes this is good Im getting the Picklist values using Describe().
    But i Want Dependent Picklist using this ,How can i get it can u please help me as soon as possible

  • Krishna

    In apex page need DropDown with list of values along with checkbox.
    please let me know the apex tag related to this or any suggestions as soon as possible.

  • kunjan

    I want to populate my dependent picklist when any changes are made in parent picklist values in dropdownlist. Please provide me code for how to get dependent picklist values. Can we pass parent picklist value in getPicklistValues()?

  • http://profile.typepad.com/mohitsharma86 mohi

    u have posted such a nice thing for beginer.
    Thanks & Regards
    mohit k sharma
    “Research comes by exploring the problem”

  • JL

    I would not exactly call this dynamic…
    Schema.DescribeFieldResult fieldResult =
    OfficeLocation__c.Country__c.getDescribe();
    The table name is hard coded… anyway to dynamically get field information? Thanks.

  • Bruce Yue

    only in this way can we get the pick list values?

  • http://cawoodm.wordpress.com/ Marc

    Is it at all possible to have apex code query the PicklistEntry’s label in another language? We need some way of copying the english labels into all other languages because the picklist values are technical keys:
    Value Label(EN) Label(DE)
    01 New Neu

    How would we query “Neu” if the current user is logged in in English?

  • Saravanan Selvamalar

    Hi All,

    Can I get the picklist values based on Record type availability value. If possible then how can
    I get the value.

  • arun

    can i change the multiple select picklist values to single picklist value