Activity Forums Salesforce® Discussions How To Get The Picklist Value In Apex Class?

  • shariq

    Member
    September 20, 2018 at 3:02 pm

    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

  • Parul

    Member
    September 20, 2018 at 5:19 pm

    Adding code snippet:

    This 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.

  • Olivier

    Member
    April 21, 2021 at 10:33 am

    Is there a possibility to get the hexadecimal color value for each picklist value ? (assuming we select a specific color for each value)

  • sumit

    Member
    August 17, 2021 at 1:31 pm

    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 3 years, 4 months ago by  sumit saini.
    • This reply was modified 3 years, 4 months ago by  sumit saini.

Log In to reply.

Popular Salesforce Blogs