Get Picklist Value In Salesforce Apex Class

Recently, I worked as a Salesforce developer for an organization that manages multiple office locations across different countries. The company uses a custom Salesforce object called OfficeLocation__c to store details about each office location, such as its address, city, state, and country. The Country__c field on the OfficeLocation__c object is a picklist that stores the office’s country.

The company added a feature to the internal Salesforce app that allows managers to filter and view office locations based on the available country options. As part of this feature, we need to dynamically retrieve the list of available country options from the Country__c picklist in Apex to display them.

In the steps below, I will explain how to get the picklist values in Salesforce Apex using the class method.

Get The Picklist Values In Salesforce Apex Class

To get the Picklist values in the Salesforce Apex class, navigate to the Salesforce developer console and follow the steps below.

1. Create a class in the developer console, and select File, New, Apex Class. Then, enter the name of the Apex class and click on the OK button.

2. Enter the code below in the created public class and save it.

public class GetPicklistValues {
    public static List<String> getCountryPicklistValues() {
       
        Schema.SObjectType officeLocationSchema = Schema.getGlobalDescribe().get('OfficeLocation__c');
        
   
        Schema.DescribeFieldResult fieldResult = officeLocationSchema.getDescribe().fields.getMap().get('Country__c').getDescribe();
        
        
        List<Schema.PicklistEntry> picklistValues = fieldResult.getPicklistValues();
        List<String> countryOptions = new List<String>();
        
        
        for (Schema.PicklistEntry value : picklistValues) {
            countryOptions.add(value.getLabel());
        }
        
        return countryOptions; 
    }
}

Let’s break the above code that we entered in the apex class to get the custom picklist field OfficeLocation__c values.

  • Schema.SObjectType officeLocationSchema will get the custom object OfficeLocation__c.
  • Schema.DescribeFieldResult fieldResult will get and describe the picklist field Country_c.
  • fieldResult.getPicklistValues(); will get the picklist values from the field.
  • In the for loopcountryOptions.add(value.getLabel()) loops through picklist values and adds them to the list.
  • Lastly, countryOptions will return the list of country options.

3. To get the output, we will execute the code and press the “Ctrl+E” shortcut keys.

4. In the anonymous window, we will call the method getCountryPicklistValues from the GetPicklistValues class.

Enter the code below and click the Execute button.

List<String> countryPicklistValues = GetPicklistValues.getCountryPicklistValues();
System.debug('Country Picklist Values: ' + countryPicklistValues);
How to get picklist values in Salesforce apex class

5. In the console log window, select the checkbox Debug only. Then, you will see the picklist values of the field Country_c.

Get Picklist Values in Salesforce Apex

This way, we can get the picklist values in the Salesforce Apex class by following the above steps.

Conclusion

In this Salesforce tutorial, we learned how to get or fetch the values of a picklist field in Salesforce Apex. By now, you will be able to manage and get the data from the picklist field in your Salesforce database more efficiently.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.