Populate the subcategory depending upon category on service portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 09:14 PM
Hi
I am trying to populate the subcategory depending upon category on service portal but its not working. It show no value in subcategory. Will you help me in this?
Thank you.
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading == 'true' || newValue == '') {
return;
}
var cat = newValue;
g_form.clearOptions('subcategory');
g_form.addOption('subcategory', '', '-- None --');
var ga = new GlideAjax('global.CatSubcat');
ga.addParam('sysparm_name', 'CatSubcat');
ga.addParam('category', cat);
ga.getXMLAnswer(getResponse);
function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
var jsonObj = JSON.parse(answer);
g_form.addOption('subcategory', jsonObj.value, jsonObj.label);
}
}
Script include:
var CatSubcat = Class.create();
CatSubcat.prototype = Object.extendsObject(AbstractAjaxProcessor, {
CatSubcat: function() {
var category = this.getParameter('category');
var jsonObj = {};
var gr = new GlideRecord('sys_choice');
gr.addQuery('name', 'incident');
gr.addQuery('element', 'subcategory');
gr.addQuery('inactive', 'false');
gr.addQuery('dependent_value', category);
gr.addOrderBy('sequence');
gr.query();
while (gr.next()) {
jsonObj.value = String(gr.value);
jsonObj.label = String(gr.label);
}
return JSON.stringify(jsonObj);
},
type: 'CatSubcat'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 09:26 PM
Hi there,
Maybe this helps:
Dependent Select Box (Choice) Variables on Record Producers (+ Multi-language!)
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 09:38 PM
Hi Mark,
This way is working but I want it on onload. I have requirenment to set the category on onload.
is there any other way?
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 09:47 PM
Hi,
follow this link and no script would be required
ServiceNow - Dependent Variables on Record Producer in Service Catalog
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2022 10:18 PM