0
public class PickListController
{


global static list<SelectOption> getPicklistValues ('Employee__c' , 'Priority__c')
{
    list<SelectOption> options = new list<SelectOption>();

    // Get the object type of the SObject.
    Schema.sObjectType objType = obj.getSObjectType();

     // Describe the SObject using its object type.
     Schema.DescribeSObjectResult objDescribe = objType.getDescribe();   

     // Get a map of fields for the SObject
     map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 

     // Get the list of picklist values for this field.
     list<Schema.PicklistEntry> values =
     fieldMap.get(fld).getDescribe().getPickListValues();

     // Add these values to the selectoption list.
     for (Schema.PicklistEntry a : values)
     { 
        options.add(new SelectOption(a.getLabel(), a.getValue())); 
     }
     return options;
   }
 } 
2
  • any issue with your code?
    – Ratan Paul
    Commented Jan 5, 2016 at 11:00
  • Unknown property 'PickListController.Employee__c' Commented Jan 5, 2016 at 11:04

1 Answer 1

0
global static list<SelectOption> getPicklistValues(SObject obj, String fld){
    list<SelectOption> options = new list<SelectOption>();

    // Get the object type of the SObject.
    Schema.sObjectType objType = obj.getSObjectType();

     // Describe the SObject using its object type.
     Schema.DescribeSObjectResult objDescribe = objType.getDescribe();   

     // Get a map of fields for the SObject
     map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 

     // Get the list of picklist values for this field.
     list<Schema.PicklistEntry> values =
     fieldMap.get(fld).getDescribe().getPickListValues();

     // Add these values to the selectoption list.
     for (Schema.PicklistEntry a : values)
     { 
        options.add(new SelectOption(a.getLabel(), a.getValue())); 
     }
     return options;
   }
 } 

Edit

public class PickListController
{

public PickListController(){
   list<SelectOption> lst = PickListController.getPicklistValues('Employee__c' , 'Priority__c'); 
   system.debug('=============check you picklist value here====='+lst);
}
public static list<SelectOption> getPicklistValues (SObject obj, String fld)
{
    list<SelectOption> options = new list<SelectOption>();

    // Get the object type of the SObject.
    Schema.sObjectType objType = obj.getSObjectType();

     // Describe the SObject using its object type.
     Schema.DescribeSObjectResult objDescribe = objType.getDescribe();   

     // Get a map of fields for the SObject
     map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 

     // Get the list of picklist values for this field.
     list<Schema.PicklistEntry> values =
     fieldMap.get(fld).getDescribe().getPickListValues();

     // Add these values to the selectoption list.
     for (Schema.PicklistEntry a : values)
     { 
        options.add(new SelectOption(a.getLabel(), a.getValue())); 
     }
     return options;
   }
 } 

Here is a utility method which i use to fetch picklist value. Use this , pass object name , field name to fetch the values.

14
  • how to fetch the filed value dynamically Commented Jan 5, 2016 at 11:06
  • just pass object and field name. it will give you value. this is dynamic. Commented Jan 5, 2016 at 11:06
  • i didnt find the field name in ur code Commented Jan 5, 2016 at 11:07
  • String fld i am passing this as parameter and using it in code. Commented Jan 5, 2016 at 11:08
  • my filed name is email so in need to pass like String Email ?? Commented Jan 5, 2016 at 11:11

Not the answer you're looking for? Browse other questions tagged .