Salesforce – How to get picklist values to Apex / Visualforce

To show a list of values in Visualforce must create the following method Apex

public List<Selectoption> getItemsList(){
  List<SelectOption> options = new List<SelectOption>(); 
  List<Schema.Picklistentry> fieldResult = CustomObjectName__c.CustomFieldName__c.getDescribe().getPicklistValues();
  options.add(new SelectOption('', '-- select -- '));
  for(Schema.PicklistEntry f : fieldResult) {
	  options.add(new SelectOption(f.getValue(), f.getLabel()));
  }
  return options;
}

And then display it on Visualforce page

<apex:pageBlockSectionItem>
    <apex:outputLabel value="{!$ObjectType.CustomObjectName__c.fields.CustomFieldName__c.label}" />
    <apex:selectList value="{!items}" multiselect="false" id="items" size="1">
        <apex:selectOptions value="{!ItemsList}" />
    </apex:selectList>
</apex:pageBlockSectionItem>

Leave a Reply

Your email address will not be published. Required fields are marked *