Salesforce | Show picklist field value of custom object on Visualforce page

Author - Neha

We can show the picklist field value on Visualforce page in a selectlist ( dropdown) using the following code in the controller class:

public class classname {
 public List<SelectOption> selectItemsList() {   
      List<SelectOption> selectOptions = new List<SelectOption>();
      Schema.DescribeFieldResult describeResult = CustomObject__c.PickListFieldName__c.getDescribe();
      List<Schema.PicklistEntry> pickListEntries = describeResult.getPicklistValues();
            
      for( Schema.PicklistEntry eachEntry : pickListEntries) {
         selectOptions.add(new SelectOption(eachEntry.getLabel(), eachEntry.getValue()));    
      }
      return selectOptions;
  }
}

In Visualforce page, we can use this using select list tag like this:

<apex:page controller=”classname”>
<apex:form>
<apex:selectList value=" " size="1" multiselect="false"  >
<apex:selectOptions value="{!selectItemsList}">
</apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at salesforce@webners.com

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

2 responses on “Salesforce | Show picklist field value of custom object on Visualforce page

  1. Satish says:

    how can we save if a user selects a specific option to that object

    • Create an apex variable in the controller with getter and setter to store the selected picklist value.
      <apex:selectList value="{!apexVariableForSelectValue}" size="1" multiselect="false" >
      <apex:selectOptions value="{!selectItemsList}">
      </apex:selectOptions>
      </apex:selectList>

      On Form submit (create a command button to submit form), you will get the selected value in the apex variable “apexVariableForSelectValue”. Use this value to store in any object you want.

Leave a Reply

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