Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
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. – user917 Jan 8 '13 at 19:30
up vote 27 down vote accepted

Salesforce doesn't reveal information about dependent picklists in normal Describe calls in Apex, but it is included in calls via the API.

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 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);
share|improve this answer
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 Jan 16 '13 at 2:22
@DerekLansing, I never got your email, but I've made a similar change to the gist. – Benj Jan 30 '13 at 20:22
Hi I am facing an issue same type. Dependent values are not coming for corresponding controlling values. Can you please help ?… – SFDC_Learner Jun 25 '15 at 9:45

So, this awesome dude, wrote exactly what you were looking for. Might as well pimp his code and spread the word. This was a life save for something I was working on this week!

share|improve this answer

I had the same problem a while ago and I have posted on ideaexchange:

At this point in time this is the only solution that I have found:

Hope that helps!

share|improve this answer

you can take help from this url where apex class is there to ceate the logic and just need to replace the custom object with the object on which you want to work for dependent picklist and to implement that logic we have a VF page so I guess that will help you alot.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.