Get lists of dependent picklist options in Apex

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

    I've tried using getPicklistValues(), but there doesn't seem to be any way of getting dependency information.



    Fruit, Vegetable, Dairy


    (Fruit): Apple, Banana, Pear

    (Vegetable): Tomato, Egglant, Lettuce

    (Dairy): Milk, Cheese, Yogurt

    What I'm Looking For

    (In pseudocode, something like:)

    controllingOptions = dependentfield.getPicklistValues();
    for (option : controllingOptions) {
        dependentOptions = dependentfield.getPicklistValuesFor(option);

    Seems like fairly major oversite on the apex side but understandable since the apex and api teams are different. I'm probably bias since I'm writing apex but I don't think there should be anything you can do in the api that you can't do in apex.

  • Benj

    Benj Correct answer

    9 years ago

    Salesforce doesn't reveal information about dependent picklists in normal Describe calls in Apex, but it is included in calls via the API, and can also be accessed through Apex by serializing and deserializing PicklistEntry objects (see gist linked below for an example of how to do that).

    Each PicklistEntry in a dependent field has a property called validFor which contains a Base64 encoded string. When decoded to bits, each bit read left to right corresponds to an option in the controlling field. For example, if you have a validFor of "gAAA"

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

    As such, you can use loop through all of the picklist values and use bitwise operators to check whether each dependent value is valid for each controlling value.

    I wrote a javascript function and Apex Class to abstract all this, which is available as a gist. You can use it like this (assumes you've correctedly loaded the Ajax toolkit in a visualforce page):

    /* Build an Object in which keys are valid options for the controlling field
     * and values are lists of valid options for the dependent field.
    var OBJ_NAME = 'Custom_Object__c';
    var CTRL_FIELD_NAME = "Controlling_Field__c";
    var DEP_FIELD_NAME = "Dependent_Field__c";
    var options = getDependentOptions(OBJ_NAME, CTRL_FIELD_NAME, DEP_FIELD_NAME);

    I sent you an update to your gist code in email. It handles the case where a dependent value is assigned to more than one master value.

    @DerekLansing, I never got your email, but I've made a similar change to the gist.

    Hi I am facing an issue same type. Dependent values are not coming for corresponding controlling values. Can you please help ?

    FYI I've updated the linked gist with a newer, much simpler algorithm (based on the same approach described in this answer) which is also available as native Apex code.

    This is a great solution! The only issue is that it does not take into account checkbox controlling fields. Picklists can be controlled by other picklists and checkbox. This gist is a slight update on your 2017 version that takes into account checkboxes. (The formatting got kind of wonky)

    Big thank-you for posting this. I did notice that for the case of the default state and country picklists where there are 330 states these have only 9 different `validFor` strings so caching those 9 outcomes reduces the generation time from 1400 ms to 150ms.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM