Dynamically Hide Sub Category field if there are no options to select
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
8 hours 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