Thursday, July 12, 2012

MSCRM 2011: Setting the values of a Pick List (Option Set) based on another Pick List (Option Set)

A Client recently asked – Can an option set’s contents be dynamic and dependent on the option selected in another option set? The answer is absolutely “yes it can be done” and below are the steps.

There are multiple steps/tasks that must be performed in order to change the values of an Option Set based on a selection in another Option Set. In the Example below, we will assume that the first Option Set is for a list of Parts and the second Option Set is for a list of Sub-Parts for those Parts.

Task 1 – Create the two Option sets (Pick Lists) and include all of the possible options.  Add them to the entity form.

Task 2Create a new JavaScript Library (Web Resource) that will include the function to build a Pick List based on a value in another Pick List.

In the java script code below:
    • Replace “new_partspicklist” with the name of your first picklist.
    • Replace “newpartssubpicklist” with the name of your dynamic picklist.
    • In the line “if(picklistOneSelectedValue == "Part I")” replace Part I with your first selection.
    • In the line “if ( picklistTwo.Options[i].Text=="Part II – Brake " || picklistTwo.Options[i].Text=="Part II – Gear shift”  || picklistTwo.Options[i].Text=="Part II - Tire")” replace each value with the values you want to remove on your first selection.
    • You will have to repeat the steps above for each value you want to filter on. The “//” lines show where each filter block begin and end. You can copy/paste to add more for more values or remove blocks if you have fewer values. Make sure you add or remove the entire code block from beginning to end.

* Go to Settings, Customizations, Customize the system.

* Scroll Down to Web Resources, Click New from the toolbar.

* Enter the following Information in the New Web Resource Dialog:

* Name= “BuildSubPartPicklistBasedOnPart”

* Display Name= “Build Sub-Part Pick List Based on Part”

* Language = “English”

* Type = “Script (Jscript)”

* Click the Text Editor button

* Paste the following script in to the source dialog and make the changes described in the Note comments.

function PartPicklistOneOnchange(context) {
 
var picklistOneAttribute = "new_partspicklist"; // replace with your name of the first picklist

var picklistTwoAttribute = "new_partssubpicklist";  //replace with the name of the picklist with dynamic values

var picklistOne = document.getElementById(picklistOneAttribute);

var picklistTwo = document.getElementById(picklistTwoAttribute);

//Getting the selected value from Picklist One

var picklistOneSelectedValue = picklistOne.SelectedText

//This "if" statement stores the original values from the dynamic picklist.
            //Very important if the user hasn't made a selection on the first picklist or if the selection changes

if (picklistTwo.flag == true) {

picklistTwo.Options = picklistTwo.originalPicklistValues;

} 

else

{

picklistTwo.originalPicklistValues = picklistTwo.Options;

picklistTwo.flag = true;

}

if (picklistOneSelectedValue != null) 

{

for (var i = picklistTwo.length - 1; i >= 0; i--) { 

    if (picklistTwo.Options[i].DataValue != null && picklistTwo.Options[i].DataValue != "")

    {               

    //BEGIN: If the picklist is set to Part I

    if(picklistOneSelectedValue == "Part I") {

      //Remove the values

      if ( picklistTwo.Options[i].Text=="Part II- Brake" || picklistTwo.Options[i].Text=="Part II - Gear Shift" || picklistTwo.Options[i].Text=="Part II - Tire")                  

      {  

          picklistTwo.remove(i); 

       }                

     }

    //END: Part I Selection     

    //BEGIN: If the picklist is set to Part II   

    if(picklistOneSelectedValue == "Part II") {

      //Remove these values 

      if ( picklistTwo.Options[i].Text=="Part I - Brake" || picklistTwo.Options[i].Text=="Part I - Gear Shift" || picklistTwo.Options[i].Text=="Part I - Tire")

      { 

            picklistTwo.remove(i);   

       }

       }

     //END: Part II Selection

}
}
}
} //End function

* Click OK

* Click Save.

* Click Publish

* Click Save and Close

Task 3 -  Attach JScript function to onChange event handler

Next we need to setup the event handlers on the form being modified.

* Click on the Entities to expand.

* Select the Entity where the function will be called from and then click OK.

* Expand the Entity, choose Form and Information form type Main.

* Now that you are in the form editor we need to attach the event handler to the Field’s onChange event.

* Go to the field that will be changed and double-click. This will bring up the field properties dialog.

* Go to the Events tab and choose to Add a Forms Library

* The Web Resource Lookup will be displayed, choose the Web Resource added above and Click OK.

* In the Event Handler section Click Add.

* In the Handler Properties dialog do the following: Function = PartPicklistOneOnchange * Check Enabled
 *Check Pass execution context as first parameter
 *Click OK, to close the Handler Properties dialog

* Click OK, to close the Field Properties dialog

* Click Save and then Publish All Customizations.

Task 4 – Test your Changes.

2 comments:

sarahlee880 said...

All life demands struggle. Those who have everything given to them become lazy, selfish, and insensitive to the real values of life. The very striving and hard work that we so constantly try to avoid is the major building block in the person we are today. See the
link below for more info.

values @
www.inspgift.com

Leslie Lim said...

I will keep your new article. I really enjoyed reading this post, thanks for sharing.

John
www.imarksweb.org