Skip to content

Commit ce798cc

Browse files
Code Snippets for hiding the dependent choice field if there are no dependent choices (#1732)
* Create README.md * Create NumberOfDependentChoices.js Script Include to find number of dependent choices of selected choice * Create HideDepnedentField.js Client script to Hide the field if there are no dependent choices for parent field choice value * Add files via upload Client script Screenshot * Update README.md
1 parent 4d95501 commit ce798cc

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2+
var fieldToHide = 'subcategory'; // I have taken subcategory as an example
3+
if (newValue === '') {
4+
g_form.setDisplay(fieldToHide, false);
5+
return;
6+
}
7+
var ga = new GlideAjax('NumberOfDependentChoices');
8+
ga.addParam('sysparm_name', 'getCountOfDependentChoices');
9+
ga.addParam('sysparm_tableName', g_form.getTableName());
10+
ga.addParam('sysparm_element', fieldToHide);
11+
ga.addParam('sysparm_choiceName', newValue);
12+
ga.getXMLAnswer(callBack);
13+
14+
function callBack(answer) {
15+
g_form.setDisplay(fieldToHide, parseInt(answer) > 0 ? true : false);
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var NumberOfDependentChoices = Class.create();
2+
NumberOfDependentChoices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
getCountOfDependentChoices: function() {
4+
var dependentChoiceCount = 0;
5+
var choiceName = this.getParameter('sysparm_choiceName');
6+
var tableName = this.getParameter('sysparm_tableName');
7+
var element = this.getParameter('sysparm_element');
8+
var choiceCountGa = new GlideAggregate('sys_choice');
9+
choiceCountGa.addAggregate('COUNT');
10+
choiceCountGa.addQuery('dependent_value',choiceName);
11+
choiceCountGa.addQuery('inactive','false');
12+
choiceCountGa.addQuery('name',tableName);
13+
choiceCountGa.addQuery('element',element);
14+
choiceCountGa.query();
15+
while(choiceCountGa.next()){
16+
dependentChoiceCount = choiceCountGa.getAggregate('COUNT');
17+
}
18+
return dependentChoiceCount;
19+
},
20+
type: 'NumberOfDependentChoices'
21+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Hide the dependent choice field when there are no available options for the selected parent choice.
2+
3+
For example, if a selected category on the incident form has no subcategories, then the subcategory field should be hidden.
4+
5+
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.
6+
7+
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)

0 commit comments

Comments
 (0)