Get Dependent Picklist Values in Apex?

Get Dependent Picklist Values in Apex?

On June 19, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Get Dependent Picklist Values in Apex?
Get Dependent Picklist Values in Apex

Question

I have a pair of dependent picklists in Salesforce. In Apex code, how can I determine the valid options in the dependent field for each value in the controlling field?

For example, consider the following picklists:

Controlling Field (Controlling_Field__c):

  • Fruit
  • Vegetable
  • Dairy

Dependent Field (Dependent_Field__c):

  • If Fruit: Apple, Banana, Pear
  • If Vegetable: Tomato, Eggplant, Lettuce
  • If Dairy: Milk, Cheese, Yogurt

I tried using getPicklistValues(), but it does not provide dependency information. Ideally, I need something like this (in pseudocode):

controllingOptions = dependentField.getPicklistValues();
for (option : controllingOptions) {
    dependentOptions = dependentField.getPicklistValuesFor(option);
    doSomethingWith(dependentOptions);
}

How can I retrieve this dependency mapping in Apex?

Answer

Salesforce does not expose picklist dependencies through standard Apex Describe calls. However, this information is available via the Metadata API and in the validFor property of PicklistEntry objects.

CRS Info Solutions offers expert-led Salesforce training in Bangalore  with certification support, and interview preparation for Admin, Developer, and AI modules—join our free demo class and boost your career.

Each dependent field’s PicklistEntry has a validFor property, which is a Base64-encoded string. When decoded into bits, each bit (read from left to right) corresponds to a value in the controlling field. A bit set to 1 means the dependent value is valid for the corresponding controlling value.

For example, if pke.validFor returns "gAAA", its bit representation is:

example pke.validFor: g      A      A      A
displayed as bits:    100000 000000 000000 000000
rearranged as bytes:  10000000 00000000 00000000

Explanation:

The validFor property is a Base64-encoded bitmask where each bit represents whether a dependent picklist value is valid for a corresponding controlling value. When decoded, the bits are read from left to right, and each 1 indicates a valid mapping between the dependent and controlling picklist values.

To extract dependencies, loop through all dependent picklist values and use bitwise operations to check which controlling values they map to.

Here’s an example Apex class that does this:

public class PicklistDependencyUtil {
    public static Map<String, List<String>> getDependentOptions(String objName, String ctrlField, String depField) {
        Map<String, List<String>> dependencies = new Map<String, List<String>>();
        
        Schema.DescribeFieldResult ctrlDescribe = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap().get(ctrlField).getDescribe();
        Schema.DescribeFieldResult depDescribe = Schema.getGlobalDescribe().get(objName).getDescribe().fields.getMap().get(depField).getDescribe();

        List<Schema.PicklistEntry> ctrlEntries = ctrlDescribe.getPicklistValues();
        List<Schema.PicklistEntry> depEntries = depDescribe.getPicklistValues();

        for (Schema.PicklistEntry ctrlEntry : ctrlEntries) {
            dependencies.put(ctrlEntry.getLabel(), new List<String>());
        }

        for (Schema.PicklistEntry depEntry : depEntries) {
            Blob decoded = EncodingUtil.base64Decode(depEntry.getValidFor());
            for (Integer i = 0; i < ctrlEntries.size(); i++) {
                Integer byteIndex = i / 8;
                Integer bitIndex = 7 - (i % 8);
                if ((decoded[byteIndex] >> bitIndex & 1) == 1) {
                    dependencies.get(ctrlEntries[i].getLabel()).add(depEntry.getLabel());
                }
            }
        }
        return dependencies;
    }
}

Explanation:

This Apex class extracts dependent picklist values for a given controlling picklist in a Salesforce object. It first retrieves the picklist values for both the controlling and dependent fields using Schema.DescribeFieldResult. Then, it decodes the validFor property of each dependent picklist value, which is a Base64-encoded bitmask, to determine which controlling values it is valid for.

To get dependent picklist values in Apex, use the Schema.DescribeFieldResult methods. First, retrieve the controlling field’s values using getPicklistValues(), then filter the dependent field’s values with getDependentPicklist() based on the controlling field selection. This ensures dynamic retrieval of valid picklist options programmatically.

Transform Your Career with Salesforce Training in Bangalore

Unlock the doors to a successful career with our industry-driven Salesforce training . Tailored to help you become an expert in the Salesforce ecosystem, our program offers in-depth training across Admin, Developer, and AI tracks, along with expert guidance for certification and thorough interview preparation.

Our hands-on learning approach equips you with practical skills, ensuring you’re ready to tackle real-world challenges and stand out in the job market. Salesforce training in Bangalore With comprehensive class materials, personalized mentorship, and a focus on practical learning, we provide everything you need to excel.

Take the first step towards transforming your career—join our free demo class today and discover how our expert-led training can make a difference!!!

See Also : Interfaces in Salesforce Apex

Variables in Apex programming in Salesforce



Comments are closed.