Activity Forums Salesforce® Discussions How to get the picklist value in Salesforce Apex class?

  • Manpreet

    Member
    May 22, 2017 at 5:58 am

    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.

  • shariq

    Member
    September 19, 2018 at 12:30 pm

    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!

  • Parul

    Member
    September 20, 2018 at 5:20 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

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos