Javascript to create dynamic cascading picklists in Microsoft CRM 2011
Changing a
picklist field value based on the other picklist field value
function caseandsubcase()
{
var
picklistOneName = "new_case13012012";
//name of the first picklist
var
picklistTwoName = "new_subcase13012012"; //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 == "Family1") {
//Remove these values
if (optionText == "Son2" || optionText == "Daughter2") {
picklistTwo.removeOption(optionValue);
}
}
//END: HMO Selection
if (picklistOneSelectedText == "Family2") {
//Remove these values
if (optionText == "Son1"
|| optionText == "Daugher1") {
picklistTwo.removeOption(optionValue);
}
}
//END: PPO Selection
}
}
}