From Picklist to SelectOption

There are occasions in which you want to build an user interface that will not save records and it just interacts with the user input data. Something like a search engine or a currency converter app. In some cases, it is better to use SelectionOption than Picklist in your app.

We can easily do this by using the Schema system class. As we know the Schema class “Contains methods for obtaining schema describe information”.
In this class, we can see how to pass all the values from a picklist to a List of SelectionOption.

*    From Picklist to SelectionOption
public without sharing class  ConverterSupportClass {

    public List<SelectOption> CurrencyOptionsOne {get;set;}
    public List<SelectOption> CurrencyOptionsTwo {get;set;}

    static void ourMethod(){
        // Getting all values from currency picklist on Rate_Code__c
        CurrencyOptionsOne = new List<SelectOption>();
        CurrencyOptionsTwo = new List<SelectOption>();
        // Use DescribeFieldResult object to retrieve currency field.
        Schema.DescribeFieldResult statusFieldDescription =  Rate_Code__c.Currency__c.getDescribe();

         * For each picklist value, create a new select option
        for (Schema.Picklistentry  picklistEntry: statusFieldDescription.getPicklistValues())
            CurrencyOptionsOne.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
            CurrencyOptionsTwo.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));


Leave a Reply

Your email address will not be published.