The idea is to filter optionset2 values based on selected value from optionset1. In my request I had a specific condition for filter but I'm sure you can use the same idea in your exact scenario.
function FilterOptionset2ByOptionset1() {
/* Get references to the related fields*/
var ParentField = Xrm.Page.data.entity.attributes.get("new_case_mainsubject");
var ChildField = Xrm.Page.data.entity.attributes.get("new_case_subsubjects");
/* Capture the current value of the child field*/
var CurrentChildFieldValue = ChildField.getValue();
var parentValue = ParentField.getValue();
/* Find the Options that corresponds to the value of the parent field. */
if (parentValue != null) {
var controls = ChildField.controls.get();
var bCurrentValueIsValid = false;
/*Enable the field and set the options*/
for (var ctrl in controls) {
controls[ctrl].setDisabled(false);
controls[ctrl].clearOptions();
var existingOptions = controls[ctrl].getAttribute().getOptions();
for (eo = 0; eo < existingOptions.length; eo++) {
//The condition for showing optionset2 value based on optionset1 value. The condition that I set bellow is just for test, you need to change it based on your needs.
if (parseInt(existingOptions[eo].value) == parseInt(parentValue)) {
controls[ctrl].addOption(existingOptions[eo]);
//if the current child selected value is in the filtered option list
if (CurrentChildFieldValue == existingOptions[eo].value) {
bCurrentValueIsValid = true;
}
}
}
}
/* If the value is valid, set it. If not, set the child field to null */
if (bCurrentValueIsValid) {
ChildField.setValue(CurrentChildFieldValue);
}
else {
ChildField.setValue(null);
}
ChildField.setSubmitMode("always");
ChildField.fireOnChange();
}
else {
//don't do any filter
var controls = ChildField.controls.get();
for (var ctrl in controls) {
controls[ctrl].setDisabled(false);
controls[ctrl].clearOptions();
var existingOptions = controls[ctrl].getAttribute().getOptions();
for (eo = 0; eo < existingOptions.length; eo++) {
if (existingOptions[eo].value != "null") {
controls[ctrl].addOption(existingOptions[eo]);
}
}
}
}
}
No comments:
Post a Comment