Dynamic Picklist – Sub Industry based on Industry aka Cascading Picklists in Microsoft CRM


This contains sample code that demonstrates how to implement dynamic picklists on a custom entity. It creates a custom entity named “Dynamic Picklist Test” that contains Industry and Sub-Industry picklist fields. When a value for Industry is selected, the values in the Sub-Industry picklist field are filtered. If there are no sub-industries for an industry, the Sub-Industry field is disabled.



See how the Sub-Industry Field isn't even an option. 🙂




The pick list for the accountants.



The picklist for the consulting industry.


 


The sample files are not intended to be used in a production environment without prior testing. You should deploy this sample to a test environment and examine it for interaction or interference with other parts of the system.


Click here for the download. And READ the ReadMe.doc file!!!


Comments (33)
  1. Ben –

    Did CRM drop the ‘originalPicklistValues’ property in the final release? – This sample code isn’t working as expected –

    Do you know of any samples that are not subject to making the picklist values sequential? ie if industry = 1 then subindustry can be 3,9,12 or 15 – rather than limiting it to subindustry would be 1-5 –

    Thanks for providing the sample – Thanks for sharing.

    Scott.

  2. Ben Vollmer says:

    Scott,

    I am not sure what feature that is. I didn’t really touch the JScript until RTM….

    I am sure that what you want to do could be handled, it would just take a little bit more work…

    I am busy on about 3 different project that all end after Convergence, so hit me up after Convergence. 🙂

    Thanks

    Ben

  3. Hi,

    The error was as following "An error occurred when populating Microsoft CRM" can  tell me if ‘originalPicklistOptions is this really valid for a picklist.

    Thanks for your time,

    Best regards,

    Michel DEGREMONT.

  4. El del CRM says:

    El otro día me plantearon esta cuestión ¿Cómo puedo hacer que los valores de un picklist (lista desplegable)

  5. CRMMario says:

    I have a problem when try to use this code:

    // re-use this code by manually firing the

    //event.

        new_industry_onchange0();

    to fire this event manually in a external page:

    this work:

    window.opener.document.forms[0].all.new_dirswap.DataValue=’Texto’;

    but this line give me a JavaScript Error:

    window.opener.document.forms[0].all.new_dirswap_onchange0();

    ??????????????????????????????

  6. Muna Hamdi says:

    I would like to have multiple choice using Picklist so to choose more than one into a list

    Any ideas?

  7. Denzel says:

    This is wonderful, my only questions are can this be applied to a 3rd level of picklists, so person choses a, which updates list b and when value from b is chosen it controls list c?

    and

    Where in your code do you actually assign which values are tied to to which values?

  8. Mike Feingold says:

    I can’t download the zip file – is this link working?

  9. David Horn says:

    I’d love to get a copy of this but I can’t download the zip file and the code no longer seems to be available on this site.  Can someone help?

  10. David Horn says:

    I’ve installed the dynamic picklist from the CRM 3.0 SDK.  I’ve installed it directly from the XML file as well as created a separate entity and re-created it there and copy pasted edited Jscript into the new entity so that it works with my field names and my drop-down picklist items.

    They both work just (the original default test entity as well as my edited entity) fine with one fairly major bug.  On either entity when you select your industry it pulls the correct index for subindustry and displays these for the user to select from.  When the user selects the desired subindustry, it indeed enters the correct information in both fields.  Once you save the record the correct information is stored.  Here is the problem however, once the record is then opened again the industry contains the correct text, the sub industry field incorrectly displays what ever text was at the top of the index.  As an example, in the default test installation of the dynamic picklist component, if accounting were selected as the industry type and Tax Returns selected as the sub industry type the record would be stored in this way.  When reentering the record industry would display as accounting but sub industry would display as auditing. If you close out of the record without saving of course the subindustry field still contains tax consultant but any time you enter the record it displays the incorrect information and of course the possibility always exists that the user could save and modify the record with the incorrect sub industry information.

    Have you seen this before, I’ve got to think that this is going to happen for everyone as it happens with the default code in a default installation without any modification whatsoever.  I guess there could also be a problem with my CRM installation.  I’m not sure, I’m going to try this on another PC as well as the problem may be due to the fact that I’m running a Vista client.  I’ll try this on a Windows XP box as well and see if I get the same problem.  I just wanted to see if anyone has seen this before.

  11. Ben Vollmer says:

    My personal website was down, which lead to this issue. 🙂 Back up…

  12. John Edwards says:

    You are right, there is an issue.

    What I did was quite different because I created my own example for this, but you can probley see here..

     crmForm.all.new_item.origSelectedText = crmForm.all.new_item.SelectedText;

    And then later in my on change code, I basically made sure that if the new subset contained the same text, set a flag = to true, and also set a variable = to the DataValue of the field I wanted to remember.

    I then made the last statement, set the DataValue of the picklist to the value I temporarly stored.

    I can send some examples of my files if you want.

  13. sj says:

    John

    Could you please email your code to correct the issue.

    Thanks

  14. Denzel says:

    So with the values in place the second picklist default to the first item on the list, is there a way that a default can be set that the second list value is blank no matter what first value is selected?

  15. Heather T says:

    Thanks for the review.  I have successfully implemented the code and now I have to manage changes to the lists.  How can I add to the list without having to keep the options in sequence?  For example items 1-5 are for A and 6-10 are for B,  But now the business want to add an additional item #11 for A.

    Taking this one step farther, perhps the solution above would allow me to "expire" a value.  For example #2 is no longer an option to be selected, but we want to show it and how it relates to older records.

    Thanks for the continued great content.

  16. gabe says:

    John

    Could you please email me also your code to correct the issue.

    Thanks

  17. gabe says:

    Hi sj,

    Could you please send me also the code on this issue.

    Thanks gabe

  18. Denzel says:

    So I am still running into this issue:

    So with the values in place the second picklist default to the first item on the list, is there a way that a default can be set that the second list value is blank no matter what first value is selected?

    I have modfied the field names, to match the needs of our particular use, but no other changes to the code have been made.  Yet everytime I save the form that the fields are a part of, the second list reverts back to the first option associated with the first list.

    I did find that if I removed the

    {new_highlevel_onchange0()} from the main form code, that the value will stay but the low level list now shows all the values vs. the filtered values.

  19. Roya says:

    John

    Could you please email your code to correct the issue.

    Thanks

  20. Michele says:

    John and/or Ben,

    I am having the same issue with the secondary drop down reverting to the first value in the sub industry list when the entity is reopened.

    If you could email your code to correct this issue, I would greatly appreciate it.

  21. Michele says:

    OK so here is the code to fix the Sub Industry from shifting back to the first Sub Industry value when the record is re-opened.  There are two lines that need to be inserted into different parts of the Industry On Change Event script.  No changes need to be made to the Forms On Load event.

    Change 1:

    After:

    var oSubIndustry = crmForm.all.new_subindustry;

    Insert:

    var currentValue = oSubIndustry.DataValue;

    Change 2:

    After:

    oSubIndustry.Options = oTempArray;

    Insert:

    oSubIndustry.DataValue = currentValue;

    Here is the full on change code with the correction:

    // Get the Industry Element, since this event was fired by this element; the

    // event’s "Source Element" will contain a reference to it.

    var oIndustry = crmForm.all.new_industry;

    // Initialize the Sub-Industry indexes

    var iStartIndex = -1;

    var iEndIndex = -1;

    // Depending on what the user selects in the Industry picklist, we will

    select

    // a range of options in the Sub-Industry picklist to display.

    //

    // For the purposes of this sample, it is assumed that the display text of

    each

    // Industry will be known and will not be localized. We have also ordered the

    // options in the Sub-Industry picklist so that they are group sequentially

    per

    // Industry. This allows the code to simply define start and stop

    Sub-Industry

    // indexes for each Industry.

    switch (oIndustry.SelectedText)

    {

    case "Accounting":

    iStartIndex = 1;

    iEndIndex = 5;

    break;

    case "Consulting":

    iStartIndex = 6;

    iEndIndex = 10;

    break;

    }

    // Get a reference to the Sub-Industry picklist element for later use

    var oSubIndustry = crmForm.all.new_subindustry;

    var currentValue = oSubIndustry.DataValue;

    // If the indexes where set, update the Sub-Industry picklist

    if (iStartIndex > -1 && iEndIndex > -1)

    {

    // Create a new array, which will hold the new picklist options

    var oTempArray = new Array();

    // Initialize the index for the temp array

    var iIndex = 0;

    // Now loop through the original Sub-Industry options, pull out the

    // requested options a copy them into the temporary array.

    for (var i = iStartIndex; i <= iEndIndex; i++)

    {

    oTempArray[iIndex] = oSubIndustry.originalPicklistOptions[i];

    iIndex++;

    }

    // Reset the Sub-Industry picklist with the new options

    oSubIndustry.Options = oTempArray;

    oSubIndustry.DataValue = currentValue;

    // Enable the Sub-Industry picklist for the user

    oSubIndustry.Disabled = false;

    }

    else

    {

    // The user has selected an unsupported Industry or no Industry

    oSubIndustry.DataValue = null;

    oSubIndustry.Disabled = true;

    }

  22. adrian says:

    Had someone got this script work

    properly? It works only for the

    first transition. When the user

    traverse more than once on the parent picklist, the scripts gives an error. I operate the script from

    the parent’s Onchange editor of Dyanmics.

    var oIndustry = crmForm.all.new_parent_picklist;

    var iStartIndex = -1;

    var iEndIndex = -1;

    switch (oIndustry.SelectedText)

    {

    case "A":

    iStartIndex = 1;

    iEndIndex = 3;

    break;

    case "B":

    iStartIndex = 4;

    iEndIndex = 6;

    break;

    }

    var oSubIndustry = crmForm.all.new_child_picklist;

    var currentValue = oSubIndustry.DataValue;

    if (iStartIndex > -1 && iEndIndex > -1)

    {

    var oTempArray = new Array();

    var iIndex = 0;

    for (var i = iStartIndex; i <= iEndIndex; i++)

    {

    oTempArray[iIndex] = oSubIndustry.Options[i];

    iIndex++;

    }

    oSubIndustry.Options = oTempArray;

    oSubIndustry.DataValue = currentValue;

    oSubIndustry.Disabled = false;

    }

    else

    {

    oSubIndustry.DataValue = null;

    oSubIndustry.Disabled = true;

    }

  23. Michele says:

    Adrian,

    I have updated your code, and there is another script as well… do you have an email I can forward the scripts to?

  24. Michele says:

    Adrian,

    I have updated your code, and there is another script as well… do you have an email I can forward the scripts to?

  25. warren says:

    Can someone please send me an updated version of this code. Michele can you please send that code. I cannot figure out how to get more than one transition without the error.

    Sincerely,

    Warren

  26. warren says:

    Hehee….I forgot to put my email.

    Its frank_2910@hotmail.com

    -Warren

  27. Pooja says:

    Hi Michele,

    could you please share the code. I am also looking where i can have solution picklist and depending on selection solution, Company name will get listed in secondary picklist.

    My email id is poojavaid@gmail.com

    thanks

    Poo

  28. warren says:

    Thank you very much for the code Michele. It really helped a lot. Much appreciate it.

    Sincerely,

    Warren

  29. warren says:

    Thank you very much for the code Michele. It really helped a lot. Much appreciate it.

    Sincerely,

    Warren

  30. Anders Jeppesen says:

    Hi,

    Where is the sourcecode for this, I tried the download link, but i didn’t work.

    can someone sent it to me, or publish it on the web?

    Sincerely,

    Anders

    Email: music.anders.jeppesen@gmail.com

  31. Triggger says:

    Can any one send me the script for 3 Level dynamic picklist

    Thanks in advance

    triggger@hotmail.com

Comments are closed.

Skip to main content