I have separated my picklist values in two records types.Now I want to display all the values of my picklist on a Visualforce page, however I am unable to do so. Please help me to achieve it. Useful links and hints are much appreciated.
1
-
1Please check this link: developer.salesforce.com/blogs/developer-relations/2008/12/… – Jair Jun 9 '14 at 2:40
Add a comment
|
public with sharing class extension_user {
private String first_picklist_option = '- All -'; //first value to display in picklist
private final User user_object; //User sobject
public extension_user(ApexPages.StandardController stdController) {
this.user_object = (User)stdController.getRecord();
}
//builds a picklist of values based upon the passed information
public List<selectOption> getPickValues(Sobject object_name, String field_name, String first_val) {
List<selectOption> options = new List<selectOption>(); //new list for holding all of the picklist options
if ( first_val != null ) { //if there is a first value being provided
options.add(new selectOption(first_val, first_val)); //add the first option
}
Schema.sObjectType sobject_type = object_name.getSObjectType(); //grab the sobject that was passed
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe(); //describe the sobject
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); //get a map of fields for the passed sobject
List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
options.add(new selectOption(a.getLabel(), a.getValue())); //add the value and label to our final list
}
return options; //return the List
}
//return the picklist options for User.TimeZoneSidKey
public List<selectOption> getTimeZones() {
return getPickValues(user_object, 'TimeZoneSidKey', first_picklist_option);
}
//return the picklist options for User.LocaleSidKey
public List<selectOption> getLocales() {
return getPickValues(user_object, 'LocaleSidKey', first_picklist_option);
}
}
<apex:page standardController="User" extensions="extension_user">
<apex:sectionHeader title="Visualforce Sample" subtitle="Describe for Picklist Values" help="/help/doc/user_ed.jsp?loc=help"></apex:sectionHeader>
<apex:form >
<apex:pageBlock title="Criteria" mode="edit">
<apex:pageBlockSection title="Information" columns="1">
<apex:pageBlockSectionItem>
<apex:outputLabel value="Time Zones" for="time_zones"></apex:outputLabel>
<apex:selectList id="time_zones" size="1" title="Time Zones">
<apex:selectOptions value="{!TimeZones}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
<apex:outputLabel value="Locales" for="locale"></apex:outputLabel>
<apex:selectList id="locale" size="1" title="Locale">
<apex:selectOptions value="{!Locales}"></apex:selectOptions>
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>