"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу
Showing posts with label How to Filter a Picklist / Option set based on another Picklist / Option set using Javascript in MS Dynamics CRM 2011. Show all posts
Showing posts with label How to Filter a Picklist / Option set based on another Picklist / Option set using Javascript in MS Dynamics CRM 2011. Show all posts

Tuesday, 1 March 2011

How to Filter a Picklist / Option set based on another Picklist / Option set using Javascript in MS Dynamics CRM 2011



The following post was done long time back. So please refer this link http://msdn.microsoft.com/en-us/library/gg594433.aspx

//Please refer the above link
In MS Dynamics CRM 2011, it is possible to filter a picklist based on another. Javascript would be an ideal method to achieve this.Lets consider that we have two pick lists  - picklistOne and picklistTwo. So when we choose a value from picklistOne, the other one should populate values based on this.


Its a good way to design the picklist values prior to do this. In the following picture, the first rectangle represents picklistOne and next one represents picklistTwo. The advantage of this design is we could easily identify related items of first picklist by looking at the fist 3 digits.
For Instance, item 10001,10002 are identified as the related item of 100
For instance, first picklist could be populated with days,months and years. Hence the related values of the same would be displayed in the next picklist based on the selected value of the first picklist.
Defining the picklist options:



Next part is to write a function to filter items of second picklist based on the selected item of the first picklist.


function PicklistOneOnchange() {

    var picklistOneAttribute = "new_picklistone";
    var picklistTwoAttribute = "new_picklisttwo";
    var picklistOne = document.getElementById(picklistOneAttribute);
    var picklistTwo = document.getElementById(picklistTwoAttribute);

    var picklistOneSelectedValue = picklistOne.DataValue;
    if (picklistTwo.flag == true) {
//a varibale originalPicklistValues is used to retain
// the original values of the picklist 
        picklistTwo.Options = picklistTwo.originalPicklistValues;

    }
    else {

        picklistTwo.originalPicklistValues = picklistTwo.Options;
        picklistTwo.flag = true;
//flag-to identify when the original picklist values to be reassigned 
    }

   if (picklistOneSelectedValue != null)
    {
        for (var i = picklistTwo.length - 1; i >= 0; i--) {           
            if (picklistTwo.Options[i].DataValue != null && picklistTwo.Options[i].DataValue != "") {
                if (picklistTwo.Options[i].DataValue.substring(0, 3) != picklistOneSelectedValue) 
{
                    picklistTwo.remove(i);
//removes unrelated items and displays only related items.
                }
            }
        }
    }
}