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