How to fetch options of Picklist field using Dynamic Apex in Salesforce?

In the previous article we have learned about How to fetch all fields of Field Sets using Dynamic Apex in Salesforce which you can read from here. In this article I will tell you How to fetch options of Picklist field using Dynamic Apex in Salesforce?

In this article, we will look at a code example that demonstrates How to fetch options of Picklist field using Dynamic Apex in Salesforce?

Below is the code snippet which will fetch all options of Status Picklist field of Account Object.

// Define the object and picklist field you want to retrieve values for.
String objectName = 'Account';  // Change this to the object name as needed.
String picklistFieldName = 'Industry';  // Change this to the picklist field API name.

// Retrieve the DescribeSObjectResult for the specified object.
Schema.DescribeSObjectResult objDescribe = Schema.getGlobalDescribe().get(objectName).getDescribe();

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

// Retrieve the Schema.SObjectField for the specified picklist field.
Schema.SObjectField picklistField = fieldMap.get(picklistFieldName);

// Get the DescribeFieldResult to fetch metadata about the field.
Schema.DescribeFieldResult fieldDescribe = picklistField.getDescribe();

// Fetch all picklist values as a list of Schema.PicklistEntry objects.
List<Schema.PicklistEntry> picklistValues = fieldDescribe.getPicklistValues();

// Iterate over the picklist values and log their labels.
for (Schema.PicklistEntry entry : picklistValues) {
    System.debug('Picklist Value: ' + entry.getLabel()); // Display the label of the picklist value.
}

Let’s try to understand the code step-by-step:

  1. Lines 2-3: Define the object name (Account) and the picklist field name (Industry).
    • These values can be changed dynamically based on the object and field you want to retrieve picklist values for.
  2. Line 6: Retrieve the metadata of the specified object using Schema.getGlobalDescribe().
    • This provides a DescribeSObjectResult, which contains detailed metadata about the object.
  3. Line 9: Get a map of all fields for the specified object (Account).
    • The map’s keys are field API names, and the values are Schema.SObjectField objects.
  4. Line 12: Retrieve the Schema.SObjectField metadata for the specified picklist field (Industry).
  5. Line 15: Get detailed metadata about the picklist field using .getDescribe().
  6. Line 18: Fetch all picklist values as a list of Schema.PicklistEntry objects.
    • Each PicklistEntry represents a selectable value in the picklist.
  7. Lines 21-23: Iterate through each picklist entry and log the label (user-friendly name) of the picklist value.

Also, check out the below video on How to fetch picklist options using dynamic apex in salesforce ?

We hope now you know How to fetch options of Picklist field using Dynamic Apex in Salesforce. If you have any doubts then let us know in the comment box below.

Leave a Comment