How to make a Subcategory dependent on a category in catalog item?

The Matrix
Tera Contributor

Hi All,

we have Category and Subcategory variables, Select Box type variables in catalog item. I want to make a subcategories choices dependent on category choices. How do I achieve this? Please refer below screenshots.

TheMatrix_3-1762102383176.png

 

TheMatrix_4-1762102406863.png

 

TheMatrix_5-1762102450631.png

 

TheMatrix_6-1762102495109.png

 

 

 

 

 

 

2 REPLIES 2

MadhanMaddy
Tera Contributor

To make “Subcategory” choices dependent on the selection of “Category” in ServiceNow catalog items, you should implement a dynamic dependent selection using Catalog Client Scripts or reference qualifiers on your variable set.


Firstly start with Reference Qualifier Setup
Set a reference qualifier for the Subcategory variable based on the selected value in the Category variable.
For Select Box variables, use the Catalog Client Script approach since reference qualifiers are primarily for reference fields.

Then Use Catalog Client Script

Create an ‘onChange’ Catalog Client Script on the ‘Category’ variable. Create a new client script of type "OnChange"

if (isLoading || newValue == '') {
g_form.clearOptions('subcategory');
return;
}


var subcategoryMap = {
'Other': ['System Functionality', 'Creation of local Job Self-Family'],
'Edit Existing Localized Content': ['System Functionality'],
'Creation of localized Job Profile': ['Job Family', 'Role Profile'],
// Add all your mappings here
};

g_form.clearOptions('subcategory');

var subs = subcategoryMap[newValue];
if (subs && subs.length) {
for (var i = 0; i < subs.length; i++) {
g_form.addOption('subcategory', subs[i], subs[i]);
}
}
}
Adjust the `subcategoryMap` definition to match your values as seen in your screenshots.


By following the above steps, the subcategory list will automatically update based on the selected category, as requested.

 

This is the dynamic solution which will work in your scenario there can be other ways as well to achieve this but this will definitely work in your case. Let me know if you want to try other ways to achieve it or let me know if you want more information on this.

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards,
Madhan

RaghavSh
Mega Patron

@The Matrix  If you want to do this which choice variable follow:

https://www.servicenow.com/community/sysadmin-forum/create-a-catalog-item-with-categories-and-sub-ca... 

 

Another method:

https://www.servicenow.com/community/itsm-articles/category-subcategory-dependency-in-service-catalo... 


Please mark the answer correct/helpful accordingly.

 


Raghav
MVP 2023
LinkedIn