Get Picklist values Dynamically Without Hardcoding

Picklist fields in Salesforce contain a list of one or more items from which a user chooses a single item. They display as drop-down lists in the Salesforce user interface. In Apex, there are times when you need to get picklist values dynamically without hardcoding them. In this post, a call will be made to getGlobalDescribe() – a schema method which returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in your organization. The Schema class sObjectType which returns the token for this SObject and DescribeSObjectResult which is the data type for an sObject describe result will be used. In addition, A Schema.PicklistEntry object is returned from the field describe result using the getPicklistValues method.
For example:-

Schema.DescribeFieldResult df = Account.Type.getDescribe();
List<Schema.PicklistEntry> pe = df.getPicklistValues();

Get Only the First value/Label

The code to get either the first value or label is pasted below

String objectName = 'Position__c';
String fieldName ='Status__c';
  
Schema.SObjectType sob = Schema.getGlobalDescribe().get(objectName) ;
Schema.DescribeSObjectResult dr = sob.getDescribe() ;
Map<String,Schema.SObjectField> fields = dr.fields.getMap() ;
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
System.debug(ple[0].getvalue());

where the Object is Contact (objectName) and the field is Type__c (fieldname)

output1

If your use case is to get the label, use System.debug(ple[0].getLabel());. And if your desire is to get both the value and the label of the first option, use System.debug(ple[0].getLabel(), ple[0].getVabel());

Get All the Picklist Values Using for loop

To get all the picklist values using for loop, use this code

String objectName = 'Position__c';
String fieldName ='Status__c';
  
Schema.SObjectType sob = Schema.getGlobalDescribe().get(objectName) ;
Schema.DescribeSObjectResult dr = sob.getDescribe() ;
Map<String,Schema.SObjectField> fields = dr.fields.getMap() ;
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
//System.debug(ple[0].getvalue());
for (Integer i= 0; i < ple.size(); i++){
    System.debug('  Value: ' +ple[i].getValue());
}

The output of the code above is pasted below

output2

Get both the label and the value

, To fetch both the label and the value, use the code below

String objectName = 'Position__c';
String fieldName ='Status__c';
  
Schema.SObjectType sob = Schema.getGlobalDescribe().get(objectName) ;
Schema.DescribeSObjectResult dr = sob.getDescribe() ;
Map<String,Schema.SObjectField> fields = dr.fields.getMap() ;
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
//System.debug(ple[0].getvalue());
for (Integer i= 0; i < ple.size(); i++){
    System.debug('  Label: ' + ple[i].getLabel() +' '+'Value: ' + ple[i].getValue());
}

The output of the code above

output

Get a Particular value from the picklist Using foreach loop

Get a particular value from the picklist, for example “Pending” from the picklist use the code below

String objectName = 'Position__c';
String fieldName ='Status__c';
  
Schema.SObjectType sob = Schema.getGlobalDescribe().get(objectName) ;
Schema.DescribeSObjectResult dr = sob.getDescribe() ;
Map<String,Schema.SObjectField> fields = dr.fields.getMap() ;
Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
//System.debug(ple[0].getvalue());
for (Schema.PicklistEntry p : ple){
        if(p.getValue() == 'Pending'){
       System.debug('Your status is pending'); 
    }
}

And the output is

pending

You can also check my other posts on picklist – Save Form With Multiple Picklist fields in Lightning Component
and Create Form with Picklist fields in Lightning Component

Reference

Apex Developer Guide
Apex Reference Guide