Developers / DevOps

Querying and Managing Salesforce Picklist Fields With Apex

By Bassem Marji

Picklist fields in Salesforce are ideal for standardization, as they offer a great way to streamline data entry, guarantee consistency, and facilitate reporting. Typically, a picklist field allows a user to choose a value from a predefined list, making data entry quicker and more straightforward. Leveraging Picklist fields helps avoid inconsistent entries and typos, particularly in scenarios involving slightly varying or repetitive data entry tasks.

This article provides insightful guidelines on picklist types and their inner components. It also provides guidance on querying picklist values using the Schema Class, SOQL, dependent picklist values, and record type. Let’s start!

Picklists: A Quick Overview

A Picklist field is mainly composed of two key elements:

  1. The field defines the type and the characteristics of the picklist.
  2. The value set defines the available options, their order, the default one, and other related settings.

In Salesforce, picklists are available in three different types:

  1. Standard Picklists: Natively included in Salesforce, and consists of common fields incorporated within the standard objects. One example is the Lead Source field on the Lead object.
  2. Custom Picklists: Created by users and customized according to their needs.
  3. Custom Multi-Select Picklists: In contrast with standard and custom picklists which enable the selection of only a single value at a time, this type of picklist enables you to choose multiple values simultaneously.

Fetching Picklist Values Using Apex

Although picklists provide an intuitive, reusable, and user-friendly solution (accompanied by built-in validation), fetching their values cloaks some concerns. Let’s walk through the different methods for parsing picklist values in Apex:

Querying Picklist Values using the Schema Class

This method leverages the Schema class to collect the relevant metadata information and optimize Apex code by metadata caching. This method does not support a record-type subdivision of picklist values.

Refer to the method:

List< Schema.PickListEntry> getPickListValues(String objAPIName, String fldAPIName)

Querying Picklist Values Using SOQL

Apart from the foregoing method, an uncommon way of extracting values using SOQL, this method consumes governor-limit resources and also does not support record types.

Refer to the method:

List< PickListValueInfo > getPickListValues(String objFldAPIName)

Now, let’s dig further and try a more complex scenario…

Querying Dependent Picklist Values

This method revolves around extracting the values of a custom-dependent picklist field whose values are filtered according to the state of another field, otherwise known as the controlling field.

The screenshot below showcases a scenario exhibiting the dependency between a controlling standard field named ‘Status’ and a new custom field ‘SubStatus’ added to the Task object:

Refer to the method:

Map<Object,List< Schema.PickListEntry>> getActiveDependentPickListValues(String objAPIName, String fldAPIName)

Querying Picklist Values by Record Type

A Record Type is a handy Salesforce feature allowing to control picklist values while providing a tailored experience for business users or business processes. This method demonstrates retrieving picklist values scoped for a record type through the REST API.

Refer to the method:

List<Object> getPickListValuesByRecordType (String objAPIName, String fldAPIName)

Summary

This article presented a set of reusable code snippets that are certainly handy in the arsenal of any Apex developer. 

Mastering these methods is essential for developers aiming to maximize efficiency and control in Salesforce development, boost their productivity, and enhance user experience to achieve better outcomes in their roles.

The Author

Bassem Marji

Bassem is a certified Salesforce Administrator, a Project Implementation Manager at BLOM Bank Lebanon, and a Technical Author at Educative. He is a big fan of the Salesforce ecosystem.

Leave a Reply