Dynamic dependent picklists in Salesforce

One thing that comes up a lot in the in the #salesforce IRC channel is doing dynamic Visual Force driven off of picklists.  So, let’s buckle up and get to it.

Data Model

In this simple example we are going to make an extension to the case page.  On this page we are going to us a custom Product/Version object to display on the page.  The product list well be determined on the start/end date of the product.  And the version will be driven by the currently selected product. Product

  • Name – The name of the product
  • Currently_Supported__c – Formula based on StartDate__c and EndDate__c (Integer version of a boolean)
  • StartDate__c – The date the product should be shown (Rollup min from the version)
  • EndDate__c – The date the product should be hidden (Rollup max from the version)

Version

  • Name – The name of the version
  • Currently_Supported__c – Formula based on StartDate__c and EndDate__c (Integer version of a boolean)
  • StartDate__c – The date the version should be shown
  • EndDate__c – The date the version should be hidden
  • Product__c – The product the version is related to

Apex Controller

These methods are simple util methods to get product information and version information

Could not embed GitHub Gist 3077272: API rate limit exceeded for 50.56.102.206. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)

The controller has the getters and setters for the product and version.  But most importantly the getters for productList and versionList.  The versionList is triggered off the record’s product.  The other part of this is that for whatever reason (I couldn’t find a good one) is the getRecord does not include the changes made to the Product__c and Version__c field, so you’ll need to set them by hand in the doSave method.

One thing to note is since this is all done in the controller extension and since get and set use the Id, the select list will have the correct thing set when editing an existing record.

Visual Force Page

The key parts of this is that the actionRegion surrounds both the item changing (product) and the dependent item (version).  If you had a third picklist you wanted to trigger on you could add another actionSupport item and tell it to rerender that third list.

Conclusion

Dependent picklists are not very hard to do as long as you remember the actionRegion around both the source and target, and making sure to get the data from the picklist prior to upserting your record.

This entry was posted in developement, salesforce and tagged , , , , , , , . Bookmark the permalink.
  • Malcolm Mackay

    Hi Patrick,

    I am trying something similar on an custom opportunity – I want to build a product picklist based upon certain criteria (fixed value based upon the type of opportunity created)

    Apologies in advance for any stupid questions – new to Apex but not programming.

    Malc

  • http://www.deadlypenguin.com pcon

    You can do this with any thing that you want. The key takeaways from this are that you:

    1. Use custom Visualforce
    2. Wrap your list in an actionRegion
    3. Return a List<SelectOption> for your getter in your controller

    Feel free to jump into the IRC channel an the folks in there can help with any specific issue you have.

  • hareesh

    Dynamic dependent pick list using apex : i created one Location__c object in that i Created two Pick Lists Country__c and State__c in Country__c pick list i added India,pakistan valus and in state__c pick list i added Andrapradesh,Madhyapradesh,lohore,Quetta if i select a value as India from Country__c pick list in State__c only able to see Andrapradesh,Madhyapradesh if i select a value as pakistan from country_c picklist in state__c only able to see lohore,Quetta by using apex

  • http://about.me/patrick.connelly Patrick Connelly

    I would need more data and some of your code to see what is going on here. Feel free to join the #salesforce IRC channel and I can try to help you, or post your problem on Stackexchange or the developer boards.