diff --git a/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/HideDependentChoiceClientScript.png b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/HideDependentChoiceClientScript.png new file mode 100644 index 0000000000..2e57a81203 Binary files /dev/null and b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/HideDependentChoiceClientScript.png differ diff --git a/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/HideDepnedentField.js b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/HideDepnedentField.js new file mode 100644 index 0000000000..bb0f64875c --- /dev/null +++ b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/HideDepnedentField.js @@ -0,0 +1,18 @@ +function onChange(control, oldValue, newValue, isLoading, isTemplate) { + var fieldToHide = 'subcategory'; // I have taken subcategory as an example + if (newValue === '') { + g_form.setDisplay(fieldToHide, false); + return; + } + var ga = new GlideAjax('NumberOfDependentChoices'); + ga.addParam('sysparm_name', 'getCountOfDependentChoices'); + ga.addParam('sysparm_tableName', g_form.getTableName()); + ga.addParam('sysparm_element', fieldToHide); + ga.addParam('sysparm_choiceName', newValue); + ga.getXMLAnswer(callBack); + + function callBack(answer) { + g_form.setDisplay(fieldToHide, parseInt(answer) > 0 ? true : false); + } + +} diff --git a/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/NumberOfDependentChoices.js b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/NumberOfDependentChoices.js new file mode 100644 index 0000000000..71417224fa --- /dev/null +++ b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/NumberOfDependentChoices.js @@ -0,0 +1,21 @@ +var NumberOfDependentChoices = Class.create(); +NumberOfDependentChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, { + getCountOfDependentChoices: function() { + var dependentChoiceCount = 0; + var choiceName = this.getParameter('sysparm_choiceName'); + var tableName = this.getParameter('sysparm_tableName'); + var element = this.getParameter('sysparm_element'); + var choiceCountGa = new GlideAggregate('sys_choice'); + choiceCountGa.addAggregate('COUNT'); + choiceCountGa.addQuery('dependent_value',choiceName); + choiceCountGa.addQuery('inactive','false'); + choiceCountGa.addQuery('name',tableName); + choiceCountGa.addQuery('element',element); + choiceCountGa.query(); + while(choiceCountGa.next()){ + dependentChoiceCount = choiceCountGa.getAggregate('COUNT'); + } + return dependentChoiceCount; + }, + type: 'NumberOfDependentChoices' +}); diff --git a/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/README.md b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/README.md new file mode 100644 index 0000000000..80b4531a8a --- /dev/null +++ b/Client-Side Components/Client Scripts/Hide Dependent Choice field if there no dependent choices/README.md @@ -0,0 +1,7 @@ +Hide the dependent choice field when there are no available options for the selected parent choice. + +For example, if a selected category on the incident form has no subcategories, then the subcategory field should be hidden. + +The file NumberOfDependentChoices.js is a client callable script include file which has a method which returns number of dependent choices for a selected choice of parent choice field. + +HideDepnedentField.js is client script which hides the dependent choice field(ex:subcategory field on incident form) if there are no active choices to show for a selected choices of it's dependent field (example: category on incident form)