+ Start a Discussion

How to Retrieve Picklist Values

I am writing visualforce pages where I need to retrieve values from a picklist and I know how to do it this way:


public List<SelectOption> getFinalsOptions(){ List<SelectOption> Finalsoptions = new List<SelectOption>(); Finalsoptions.add(new SelectOption('null','-- Select One --')); Schema.DescribeFieldResult Finals = Schema.sObjectType.Business_Review__c.fields.Interest_in_Finals__c; for (Schema.PickListEntry FinalsPickVal : Finals.getPicklistValues()){ // create a selectoption for each pickval Finalsoptions.add(new SelectOption(FinalsPickVal.getValue(),FinalsPickVal.getLabel())); } return Finalsoptions; }


Then the visualforce looks like this:

<apex:pageBlockSectionItem > <apex:outputLabel value="Interest in Finals" /> <apex:selectList value="{!Business_Review__c.Interest_in_Finals__c}" size="1"> <apex:selectOptions value="{!FinalsOptions}" /> <apex:actionSupport reRender="brBlock" event="onchange" status="" /> </apex:selectList> </apex:pageBlockSectionItem>




The issue is that I can only have so many describe calls and I have a large number of picklist fields where I need to retreieve their values so that I can create a series of dependencies to other fields from the picklist.


So the question summed up: Is there a way to write a query that just extracts the values in a picklist.

If I have a picklist Month__c with values: jan, feb, mar, apr, may, jun, jul, etc... How do I write a query just to get the values from that field?


I have found some information about possibly using a map:

Map<String, Schema.SObjectField> M = Schema.SObjectType.Account.fields.getMap();

I am just not sure how to get the value that I need from the map. So that I can extract the picklist values.


Any suggestions - I have not been successful so far.


Thanks in advance!

Message Edited by MSVRad on 12-17-2009 12:52 PM

How about something like this:



public static String getPicklistValues(Sobject obj, String fieldName) { Schema.sObjectType t = obj.getSObjectType(); Schema.DescribeSObjectResult objDesc = t.getDescribe(); Map<String, Schema.SObjectField> fieldsMap = objDesc.fields.getMap(); return List<Schema.PicklistEntry> pickListValues = fieldsMap.get(fieldName).getDescribe().getPickListValues(); }


 You may also want to cache the Schema.DescribeSObjectResult in a Map to reduce describe calls.







Thanks for the reply.


my only issue with that snippit you provided is that you are trying to return a list, but the return value you have set is of type String - so wouldn't I need to return a string value? 


So heres another question. Is there anyway to extract the picklist values from a picklist without using the "getPickListValues()" method? I can only call this method 10 times before I hit a governor limit. But, I have nearly 30 picklists that I need to use this method on in order to get the values from the picklist.


Any insight would be greatly appreciated. I have not been able to find anything about not using the getPickListValues method thus far.



Message Edited by MSVRad on 12-21-2009 12:01 PM

Hi MsvRad,


The snippet I provided was part of a utility method that returns a single picklist value as a string; I intended it as something just to get you started.


I don't think you'll be able to get around this limit. You might try filing a case with salesforce support.


Good luck!