Skip to content

Instantly share code, notes, and snippets.

@anilsomasundaran
Created September 3, 2018 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anilsomasundaran/0a3724c877ec4ae09219a792cc271a29 to your computer and use it in GitHub Desktop.
Save anilsomasundaran/0a3724c877ec4ae09219a792cc271a29 to your computer and use it in GitHub Desktop.
How to populate the dependent optionset based on a range (child optionset value should maintain a range based on the parent option-set values) in ms crm dynamics 365
function filterOptionSet(parentOptionSet, childOptionSet, maxOptionRange) {
//Gets the parent option set control from the CRM page context
var primaryOptionSet = Xrm.Page.getAttribute(parentOptionSet);
//Reads the currently selected option in the parent option set
var selectedOption = primaryOptionSet.getSelectedOption();
//In other way, reads the control of the child option set
//where the dependent values to be populated
var dependentOptionSet = Xrm.Page.ui.controls.get(childOptionSet);
//calls the interal function for filter the child optionset values.
filterDependentOptionSet(selectedOption, dependentOptionSet, maxOptionRange);
}
function filterDependentOptionSet(selectedOption, dependentOptionSet, maxOptionRange) {
var optionValues = dependentOptionSet.getAttribute().getOptions();
dependentOptionSet.clearOptions();
if (selectedOption) {
var min = selectedOption.value;
var max = (min + maxOptionRange) - 1;
for (var index = 0; index < optionValues.length; index++) {
var optionValue = optionValues[index];
var value = optionValue.value;
if (value >= min && optionValue.value <= max) {
dependentOptionSet.addOption(optionValue);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment