- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Good Morning
I would like to add logic that the subcategory field is hidden on the incident form if there are no options configured to select.
For example:-
If the Category is "WINDOWS" and we have configured "Update Required" or "Restart" as subcategory options then the subcategory should be visible on the form.
If we had another Category "JIRA", but we have not added any subcategory options ( as they are not required), we would like the Subcategory field to be hidden.
I thought about using a ui policy, but that will not work as the will only work if a actual value is entered.
Any Ideas?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I hope yo may need 2 script one for Onchange and onLoad
Create an onChange Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Clear the current Subcategory
g_form.setValue('subcategory', '');
// Delay execution to allow subcategory options to load
setTimeout(function () {
var subcatControl = g_form.getControl('subcategory');
if (!subcatControl) return;
var options = subcatControl.options;
var optionCount = 0;
for (var i = 0; i < options.length; i++) {
var opt = options[i];
// Count only non-empty values (excluding --None-- or empty)
if (opt.value && opt.value.trim() !== '') {
optionCount++;
}
}
if (optionCount === 0) {
g_form.setDisplay('subcategory', false);
} else {
g_form.setDisplay('subcategory', true);
}
}, 100); // Delay to wait for options to load
}
Also Handle onLoad:
function onLoad() {
var category = g_form.getValue('category');
if (!category) return;
setTimeout(function () {
var subcatControl = g_form.getControl('subcategory');
if (!subcatControl) return;
var options = subcatControl.options;
var optionCount = 0;
for (var i = 0; i < options.length; i++) {
var opt = options[i];
if (opt.value && opt.value.trim() !== '') {
optionCount++;
}
}
if (optionCount === 0) {
g_form.setDisplay('subcategory', false);
} else {
g_form.setDisplay('subcategory', true);
}
}, 100);
}
Thanks,
Vignesh
"If this solution resolves your issue, kindly mark it as correct."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
you will have to write script to check based on category selected if it has subcategory or not
If not then hide the field itself.
Something like this using Ajax
Client Script: onChange of Category
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Query the sys_choice table to find if there are subcategories for the selected category
var ga = new GlideAjax('CheckSubcategoryOptions');
ga.addParam('sysparm_name', 'hasSubcategory');
ga.addParam('sysparm_category', newValue);
ga.getXMLAnswer(function(answer) {
var hasOptions = (answer == 'true');
g_form.setVisible('subcategory', hasOptions);
if (!hasOptions) {
g_form.clearValue('subcategory');
}
});
}
Script Include: It should be client callable
var CheckSubcategoryOptions = Class.create();
CheckSubcategoryOptions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasSubcategory: function() {
var category = this.getParameter('sysparm_category');
var gr = new GlideRecord('sys_choice');
gr.addQuery('name', 'incident'); // or your table name
gr.addQuery('element', 'subcategory');
gr.addQuery('inactive', false);
gr.addQuery('dependent_value', category);
gr.query();
return gr.hasNext() ? 'true' : 'false';
}
});
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
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
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
