function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhilash Mishra 13Abhilash Mishra 13 

getting picklist field options in list in apex classs

Hi, 
can i get all the values of a picklist field in a list some how?

I have picklist type custom field in the product. having some options.
In my apex class i want to use these options. can I get them some how throught code? 
Best Answer chosen by Abhilash Mishra 13
ManojjenaManojjena
Hi Abhilash,

If you want to display pick list values in your custom page through dyanamic apex .You can check below link it will help  .

https://developer.salesforce.com/blogs/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html

If you want in a string array or list then you can use below code .
 
public List<String> getPickListValuesIntoList(){
       List<String> pickListValuesList= new List<String>();
		Schema.DescribeFieldResult fieldResult = ObjectApiName.FieldApiName.getDescribe();
		List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
		for( Schema.PicklistEntry pickListVal : ple){
			pickListValuesList.add(pickListVal.getLabel());
		}     
		return pickListValuesList;
    }
Let us know if it helps !!
Thanks
Manoj
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you

    /*
    *************************************************************************************************************
    * Below method is issed to get the picklist value.
    *************************************************************************************************************
    */
    public List<SelectOption> getProduct()
    {
       List<SelectOption> options = new List<SelectOption>();
       Schema.DescribeFieldResult fieldResult = Contact.Product__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for( Schema.PicklistEntry f : ple)
       {
               options.add(new SelectOption(f.getLabel(), f.getValue()));
       }     
       return options;
    }
 
ManojjenaManojjena
Hi Abhilash,

If you want to display pick list values in your custom page through dyanamic apex .You can check below link it will help  .

https://developer.salesforce.com/blogs/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html

If you want in a string array or list then you can use below code .
 
public List<String> getPickListValuesIntoList(){
       List<String> pickListValuesList= new List<String>();
		Schema.DescribeFieldResult fieldResult = ObjectApiName.FieldApiName.getDescribe();
		List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
		for( Schema.PicklistEntry pickListVal : ple){
			pickListValuesList.add(pickListVal.getLabel());
		}     
		return pickListValuesList;
    }
Let us know if it helps !!
Thanks
Manoj
 
This was selected as the best answer
Gopi MarriGopi Marri
Hi ,

Below code will work.

 public static void getSessionLevels()
    {
        Schema.DescribeFieldResult fieldResult = Opportunity.StageName.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for(Schema.PicklistEntry s:ple){
            
            System.debug('value: '+s.getLabel());
        } 
    }
Abhilash Mishra 13Abhilash Mishra 13
Thanks Gopi for the answer but its been solved long ago.
Alon WaismanAlon Waisman

I'm guessing some of you would like a fully dynamic solution. Here's a method you can pop into a Utilities class to make life easier. I do, and my utilities class is named... wait for it... Utilities.

public class Utilities {

   public static String[] picklist_values(String object_name, String field_name) {
      String[] values = new String[]{};
      String[] types = new String[]{object_name};
      Schema.DescribeSobjectResult[] results = Schema.describeSObjects(types);
      for(Schema.DescribeSobjectResult res : results) {
         for (Schema.PicklistEntry entry : res.fields.getMap().get(field_name).getDescribe().getPicklistValues()) {
            if (entry.isActive()) {values.add(entry.getValue());}
         }
      }
      return values;
   }

}


Example:
There's a custom field on Contact called Favorite_Colors__c with these active values: 'Pink', 'Orange', 'The Whole Rainbow'

System.debug(Utilities.picklist_values('Account', 'Favorite_Colors__c')) => (Pink, Orange, The Whole Rainbow)

 

savitri kalshetty 4savitri kalshetty 4
Thanks Amit Chaudhary it helps me...
Jaap ScheperJaap Scheper
What about a generic solution that returns both Value and Label? You might want to create a neat Combobox on your Visualforce page. Here's how you do that:
 
public class Util_DML {

public static Map<String, String> picklistValues(String objectName, String fieldName) {
        Map<String, String> values = new Map<String, String>{};

        List<Schema.DescribeSobjectResult> results = Schema.describeSObjects(new List<String>{objectName});
        
        for(Schema.DescribeSobjectResult res : results) {
            for (Schema.PicklistEntry entry : res.fields.getMap().get(fieldName).getDescribe().getPicklistValues()) {
                if (entry.isActive()) {
                    values.put(entry.getValue(), entry.getLabel());
                }
            }
        }

        return values;
    }
}

Controller:
 
public String salutation { get; set; }
public List<SelectOption> getSalutationPicklist() {
        List<SelectOption> selectOptions = new List<SelectOption>();

        Map<String, String> salutationValueLabels = Util_DML.picklistValues('Lead', 'Salutation');
        for (String value : salutationValueLabels.keySet()) {
            selectOptions.add(new SelectOption(value, salutationValueLabels.get(value)));
        }

        return selectOptions;
    }

Visualforce page:
<apex:selectList value="{!salutation}" size="1" label="Salutation">
    <apex:selectOptions value="{!SalutationPicklist}"/>
</apex:selectList>

 
channel foralschannel forals
Channelforals (https://www.channelforals.org) Find the latest breaking news and information on the top stories, weather, business, entertainment, politics, and more. For in-depth coverage, Channelforals (https://www.channelforals.org) provides ...
Sumit Saini 1Sumit Saini 1
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
 
Let us know if it helps !!
Thanks
Sumit
Raj Singh 109Raj Singh 109
Picklist may be quickly seen using the UI in Salesforce Lightning mode: Object Manager in Setup Choose the object that contains the field -> Fields and Relationships should be selected. choose the field -> You may view the values by scrolling down. The "Export" button on the picklist exports the picklist's values list when pressed.

Thanks,
HWC Loan Personal