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

Picklist Value

Dear Developers,


Am generating a PDF file using Visual Force, so in the objects that is being accessed , few of the fileds are picklist can these values be obtained? Is there any specific functions need to be done to get the value of the picklist value?


Thanks in Advance



For this you will have to look at the very powerful and useful Dynamic Apex Describe Information capabilities.  These capabilities allow one to interrogate a field or sObject, and describe their characteristics. 

Please refer to the following example:-


Lets say we have a custom object called Student__c. Student__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 = Student__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 =
   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="{!Student__c.Country__c}"
         size="1" required="true">
  <apex:selectOptions value="{!countries}"/>
Hope this helps..