Using Dynamic Apex to retrieve Picklist Values
by Quinton Wall on December 9, 2008 at 10:53 AM
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>
TrackBack
TrackBack URL for this entry: http://www.typepad.com/services/trackback/6a00d8341cded353ef0105365512c4970c
Listed below are links to weblogs that reference Using Dynamic Apex to retrieve Picklist Values:

Comments
Posted by Jon Mountjoy on December 10, 2008 01:00 AM:
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
Posted by Jesper Joergensen on December 10, 2008 09:34 AM:
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.
Posted by A B on December 10, 2008 03:58 PM:
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.
Posted by Varun on February 16, 2009 03:00 AM:
exact same issue as with "A B" above. Has anyone able to use Dependent Picklists in Visualforce pages using APEX classes?
Posted by David Schach on March 16, 2009 05:37 PM:
There is a dependent picklist example in the Force.com Developer Guide, pp 373-379.
Posted by Ian Randall on March 24, 2009 07:10 PM:
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 :)
Posted by SIMHA on May 12, 2009 05:07 AM:
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
Posted by Krishna on October 28, 2009 04:32 AM:
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.
Posted by kunjan on November 8, 2009 10:32 PM:
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()?
Posted by mohi on April 2, 2010 12:18 AM:
u have posted such a nice thing for beginer.
Thanks & Regards
mohit k sharma
"Research comes by exploring the problem"
The comments to this entry are closed.