Thursday, August 23, 2012

How to Create a parent child optionSet/PickList in MS CRM 2011?

To make a Parent Child OptionSet or PickList in MS CRM 2011 use the following line of code:

Also You can change values in second OptionSet based on first OptionSet Value

function PicklistOneOnchange() {
    var picklistOneName = "new_healthplantype"; //name of the first picklist
    var picklistTwoName = "new_healthinsuranceplan";  //name of the picklist with dynamic values
   
    var picklistOne = Xrm.Page.getControl(picklistOneName);
    var picklistOneAttribute = picklistOne.getAttribute();
   
    var picklistTwo = Xrm.Page.getControl(picklistTwoName);
    var picklistTwoAttribute = picklistTwo.getAttribute();
       
       var picklistOneSelectedOption = picklistOneAttribute.getSelectedOption();
   
    var picklistOneSelectedText = "";   
    if (picklistOneSelectedOption != null)
    {
        picklistOneSelectedText = picklistOneSelectedOption.text;
    }

    //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.clearOptions();
        var origOptions = picklistTwo.originalPicklistValues;
       
        for (var i = origOptions.length - 1; i >= 0; i--)
        {
            if(origOptions[i].text != "")
            {
                picklistTwo.addOption(origOptions[i]);
            }
        }       
    }
    else
    {       
        picklistTwo.originalPicklistValues = picklistTwoAttribute.getOptions();
        picklistTwo.flag = true;
    }

    if (picklistOneSelectedText != null && picklistOneSelectedText != "")
    {       
        var picklistTwoOptions = picklistTwoAttribute.getOptions();
        for (var i = picklistTwoOptions.length - 1; i >= 0; i--) { 
           
            if (picklistTwoOptions[i].value != null && picklistTwoOptions[i].value != "") {
                var optionText = picklistTwoOptions[i].text;
                var optionValue = picklistTwoOptions[i].value;
               
                //BEGIN: If the picklist is set to HMO
                if(picklistOneSelectedText == "HMO")
                {                           
                    //Remove these values
                    if (optionText == "PPO 500" || optionText == "PPO 750" || optionText == "POS Silver" || optionText == "POS Gold")
                    {
                        picklistTwo.removeOption(optionValue);
                    }

                }
                //END: HMO Selection
               
                //BEGIN: If the picklist is set to PPO
                if(picklistOneSelectedText == "PPO")
                {
                    //Remove these values
                    if (optionText == "HMO 5K" || optionText == "HMO 7K" || optionText == "POS Silver" || optionText == "POS Gold")
                    {
                        picklistTwo.removeOption(optionValue);
                    }

                }           
                //END: PPO Selection
               
                //BEGIN: If the picklist is set to POS
                if(picklistOneSelectedText == "POS")
                {
                    //Remove these values
                    if (optionText == "HMO 5K" || optionText == "HMO 7K" || optionText == "PPO 500" || optionText == "PPO 750")
                    {
                        picklistTwo.removeOption(optionValue);
                    }

                }       
                //END: POS Selection
            }
        }
    }
   
}

No comments:

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...