Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

Example

Controlling_Field__c

Fruit, Vegetable, Dairy

Dependent_Field__c

(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);
    do_something_with_it();
}
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

3 Answers 3

up vote 14 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);
console.debug(options);
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

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

https://sites.secure.force.com/success/ideaView?id=08730000000h1y6AAA

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

http://iwritecrappycode.wordpress.com/2012/02/23/dependent-picklists-in-salesforce-without-metadata-api-or-visualforce/

Hope that helps!

share|improve this answer

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!

http://titancronus.com/blog/2014/05/01/salesforce-acquiring-dependent-picklists-in-apex/

share|improve this answer

Your Answer

 
discard

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.