- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2024 11:59 PM
Hi Community,
I had a requirement, I have department and sub-department on a catalog item which have reference qualifiers on them of type lookup select box which are all present in the cmn_department table and the sub- departments have a parent department so all the sub-departments have a department but all departments do not have a sub- department now the requirement is that if the selected department has a sub- department then only the sub-department variable should be visible and mandatory or if not the sub-department variable should not be visible. Any help?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 03:47 AM - edited 05-29-2024 03:48 AM
@Chirag Pathak If you decide to change to reference type follow below steps as I there will be few changes to the script & reference qualifier
- In department variable. (parent is empty means it is itself the parent)
- In Sub department variable
Ref Qualifier: javascript:'parent='+current.variables.YourDepartmentFieldName
Script Include:
hasSubDepartments: function() {
var subDept = new GlideRecord('cmn_department');
subDept.addEncodedQuery('parent='+this.getParameter('sysparm_department'));
subDept.query();
return subDept.hasNext();
},
On Change Client script on department variable:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('sub_department', false);
g_form.setVisible('sub_department', false);
return;
}
var ga = new GlideAjax('YourScriptIncudeName');
ga.addParam('sysparm_name', 'hasSubDepartments');
ga.addParam('sysparm_department', newValue);
ga.getXMLAnswer(function(answer) {
alert(answer);
if (answer === "true") {
g_form.setVisible('sub_department', true);
g_form.setMandatory('sub_department', true);
} else if (answer === "false") {
g_form.setMandatory('sub_department', false);
g_form.setVisible('sub_department', false);
}
});
}
Tested in PDI. If you encounter any issue let me know
I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 12:19 AM
in this case you need to have onChange client script on that department variable and then show/hide the other variable
I will suggest to use Reference variable instead of lookup select box as onChange on lookup select box won't work
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 01:16 AM
Hi @Chirag Pathak ,
Fulfilling this requirement using lookup select box involves risk in future when name dept name changes etc. Instead use reference type variables.
You can achieve the solution using lookup select box also (but not ideal). Below is the solution if you want to continue with lookup select box
In Sub department Variable add below
Reference qualifier: javascript:'parent.name='+current.variables.YourDepartmentVariableName
Variable Attributes: ref_qual_elements=YourDepartmentVariableName
Script Include:
hasSubDepartments: function() {
var subDept = new GlideRecord('cmn_department');
subDept.addEncodedQuery('parent.name='+this.getParameter('sysparm_department'));
subDept.query();
return subDept.hasNext();
},
On Change Client script on Department:
replace script include name & function name
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setVisible('sub_department',false);
return;
}
var ga = new GlideAjax('YourScriptIncludeName');
ga.addParam('sysparm_name', 'YourScriptIncludeFunctionName');
ga.addParam('sysparm_department', newValue);
ga.getXMLAnswer(function(answer) {
alert(answer);
if (answer === 'true') {
g_form.setDisplay('sub_department', true);
g_form.setMandatory('sub_department', true);
} else {
g_form.setDisplay('sub_department', false);
g_form.setMandatory('sub_department', false);
}
});
}
Tested in PDI.
I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 03:24 AM
Hi Sai,
will this work if I change the type to reference instead?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2024 03:47 AM - edited 05-29-2024 03:48 AM
@Chirag Pathak If you decide to change to reference type follow below steps as I there will be few changes to the script & reference qualifier
- In department variable. (parent is empty means it is itself the parent)
- In Sub department variable
Ref Qualifier: javascript:'parent='+current.variables.YourDepartmentFieldName
Script Include:
hasSubDepartments: function() {
var subDept = new GlideRecord('cmn_department');
subDept.addEncodedQuery('parent='+this.getParameter('sysparm_department'));
subDept.query();
return subDept.hasNext();
},
On Change Client script on department variable:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setMandatory('sub_department', false);
g_form.setVisible('sub_department', false);
return;
}
var ga = new GlideAjax('YourScriptIncudeName');
ga.addParam('sysparm_name', 'hasSubDepartments');
ga.addParam('sysparm_department', newValue);
ga.getXMLAnswer(function(answer) {
alert(answer);
if (answer === "true") {
g_form.setVisible('sub_department', true);
g_form.setMandatory('sub_department', true);
} else if (answer === "false") {
g_form.setMandatory('sub_department', false);
g_form.setVisible('sub_department', false);
}
});
}
Tested in PDI. If you encounter any issue let me know
I started answering community questions recently. If my answer helped you in any way, please mark it as helpful or correct. It would be a great boost.