Reusable #Apex Method to get Picklist Values Dynamically in #Salesforce



Hi guys,

In this article I will share a very cool code block with you to get Picklist values from apex. It's a reusable method that can be used multiple times for different objects & fields.

The method can be used in VF page or Lightning Component.

Please check the code below : 

Controller
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class PickListController {
    public static List<String> getPickListValuesIntoList(String objectType, String selectedField){
        List<String> pickListValuesList = new List<String>();
        Schema.SObjectType convertToObj = Schema.getGlobalDescribe().get(objectType);
        Schema.DescribeSObjectResult res = convertToObj.getDescribe();
        Schema.DescribeFieldResult fieldResult = res.fields.getMap().get(selectedField).getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        for( Schema.PicklistEntry pickListVal : ple){
            pickListValuesList.add(pickListVal.getLabel());
        }     
        return pickListValuesList;
    }
}


Post a Comment

0 Comments