• #35702
    Salesforce | Aman Aman #4
    Forcetalks

    How To Get The Picklist Value In Apex Class?

    How To Get The Picklist Value In Apex Class?

    #35710
    Salesforce | shariq shariq #1
    Forcetalks

    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

    #35787
    Salesforce | Parul Parul #2
    Forcetalks

    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.

Viewing 3 posts - 1 through 3 (of 3 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?