Activity › Forums › Salesforce® Discussions › How to get the picklist value in Salesforce Apex class?
Tagged: Field Value, Picklist, Salesforce Apex, Salesforce Apex Class, Salesforce Development, Salesforce SOQL
-
How to get the picklist value in Salesforce Apex class?
Posted by Saurabh on May 12, 2017 at 1:41 PMHow to get picklist value in Apex class?
Parul replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
Hi Saurabh,
To get picklist values in Apex is very simple. I am sharing some code, please go through this:
Here is My Controller
public class testclass { public Job__c job {get;set;} public List<SelectOption> options{get;set;} public testclass() { options = new List<SelectOption>(); Schema.DescribeFieldResult fieldResult = Job__c.Status__c.getDescribe(); List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); for( Schema.PicklistEntry f : ple) { options.add(new SelectOption(f.getLabel(), f.getValue())); } } }My Page
<apex:page controller="testclass"> <apex:form> <apex:pageBlock> Order Status:<apex:selectList id="countries" value="{!job.Status__c}" size="1" required="true"> <apex:selectOptions value="{!options}"/> </apex:selectList> </apex:pageBlock> </apex:form> </apex:page>Thanks.
- [adinserter block='9']
-
Hi,
Lets say we have a custom object called OfficeLocation__c. OfficeLocation__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 = OfficeLocation__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.Hope this helps!
-
Hi,
Using Dynamic apex, we can achieve this. On object of type pickilist, call getDescribe(). Then call the getPicklistValues() method. Iterate over result and create a list. Bind it to <apex:selectOptions>.
Code Example:
Let’s say we have a custom object called OfficeLocation__c. This object contains a picklist field Country__c.
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 = OfficeLocation__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.gerPicklistValues();
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 = OfficeLocation__c.Country__c.getDescribe(); 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=”{!Office_Location__c.Country__c}” size=”1″ required=”true”> <apex:selectOptions value=”{!countries}”/> </apex:selectList>Thanks
Log In to reply.