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;
}
}
-
any issue with your code?– Ratan PaulCommented Jan 5, 2016 at 11:00
-
Unknown property 'PickListController.Employee__c'– keerthana chowdharyCommented Jan 5, 2016 at 11:04
Add a comment
|
1 Answer
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.
-
-
just pass object and field name. it will give you value. this is dynamic. Commented Jan 5, 2016 at 11:06
-
-
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