Activity › Forums › Salesforce® Discussions › How To Get The Picklist Value In Apex Class?
-
How To Get The Picklist Value In Apex Class?
Posted by Aman on September 20, 2018 at 3:01 PMHow To Get The Picklist Value In Apex Class?
sumit saini replied 4 years, 9 months ago 5 Members · 4 Replies -
4 Replies
-
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
- [adinserter block='9']
-
Adding code snippet:
This is very simple. I am sharing some code ,please go through this
Here is My Controllerpublic 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.
-
Is there a possibility to get the hexadecimal color value for each picklist value ? (assuming we select a specific color for each value)
-
This reply was modified 5 years ago by
Olivier Baelde.
-
This reply was modified 5 years ago by
-
Hi,If you want to get picklist values in apex then you can check below link it will help.
https://sfdctechsolutions.blogspot.com/2021/08/get-picklist-values-in-apex.html.-
This reply was modified 4 years, 9 months ago by
sumit saini.
-
This reply was modified 4 years, 9 months ago by
sumit saini.
-
This reply was modified 4 years, 9 months ago by
Log In to reply.