Dependent picklists in Salesforce
Virtually every site I’ve worked with in recent years has had need for dependent drop downs of some nature.
Usually implementing a dependent drop down consists one of two options: Outputting every option into a JavaScript method, or invoking an Ajax call to get the next set of options. The disadvantage to both of these options is usually a bulky mechanism the developer must write in order to provide a simple UI component.
Salesforce provides a dependent picklist feature, which in keeping with their no-software mantra, eliminates any need for a custom ajax or javascript solution. Define a dependent picklist and reference it in your page layout or Visualforce page. Salesforce takes care of everything else under the covers.
Declaring the dependent picklist isn’t as straight forward as declaring a custom field, however. There is no option for dependency when creating the picklist through the UI. In order to establish dependency between 2 or more pick lists, create the pick list fields and then modify the object file.
In order to modify the object file, in the Force.com or Eclipse IDE, extract the standard or custom object from your org. Right click on your org in the IDE, go to Add/Remove Metadata Components.., select both Standard and Custom objects, and refresh your project from the server. You should see a list of object files. Open the desired project file. In my case, this was Address__c.object.

Set up screen for including Force.com meta data components in IDE project
This object file is where you need to declare the dependency between your picklist fields. For a demonstration, I created an Address__c object with two custom fields, Country and State. The State has a dependency on Country. When Country is United States, the State list contains US States. When Country is Canada, the State list contains Canadian states. When Country is neither United States nor Canada, then the State field is blank or undefined. To set up this dependency, I edited the State__c picklist field in Address__c.object. I defined a controllingField element as a first sibling of the picklist element to be Country__c. Also, for each state value, I added a controllingFieldValues element as a child of picklistvalues, indicating the State is displayed if either United States or Canada is selected from the Country__c picklist. See the example below for a better explanation.

Example of dependent picklists between Country and State
Keep in mind, this is a simple example of a dependent picklist. The dependent picklists can have more than one dependency, or support a cascading menu of options in addition to many other features. Heck, you could implement your own simple Visual process manager (Force.com flow) with dependent pick lists.
If you’re still curious, I’ve attached the example Address__c.object file (After saving, rename from Address__c.txt to Address__c.object). Also, be sure to check out the Salesforce developer docs for any further questions.
No comments yet.
Comments are closed.
