- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 05:47 AM
Hi everyone,
I have a category field that has the default value "Leadership." The category field changes the options in the "topic" field based on what is selected. I do this through an OnChange Catalog Client Script. The problem is that when I load the form, the options related to the "Leadership" field don't appear. I try to load them through this OnLoad Catalog Client Script below. Any ideas?
Thank you very much, and best regards.
function onLoad() {
loadTopicOptionsLeadership();
}
function loadTopicOptionsLeadership() {
g_form.clearOptions('topic');
var options = [
'Leadership escalation',
'Leadership recommendation',
'EHS Policy',
'Organization roles, responsibilities and authorities',
'Leadership Participation/Consultation request'
];
g_form.addOption('topic', '', '-- None --');
for (var i = 0; i < options.length; i++) {
g_form.addOption('topic', options[i], options[i]);
}
g_form.setValue('topic', options[0]);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 10:08 AM
Hi @tomaslindev
Your current OnLoad script always calls loadTopicOptionsLeadership(), regardless of the actual value in the "category" field. This is likely why you're not seeing the correct behavior when a user changes the category.
please follow steps to improve your setup
Step 1: Update loadTopicOptionsLeadership() to be reusable.
Your function looks good — we’ll just make it modular so you can call it from multiple places (OnLoad + OnChange).
Step 2: Add logic to only call loadTopicOptionsLeadership() if the selected category is "Leadership
onLoad script:
function onLoad() {
var category = g_form.getValue('category');
if (category == 'Leadership') {
loadTopicOptionsLeadership();
}
}
loadTopicOptionsLeadership() function (unchanged, just reusable):
function loadTopicOptionsLeadership() {
g_form.clearOptions('topic');
var options = [
'Leadership escalation',
'Leadership recommendation',
'EHS Policy',
'Organization roles, responsibilities and authorities',
'Leadership Participation/Consultation request'
];
g_form.addOption('topic', '', '-- None --');
for (var i = 0; i < options.length; i++) {
g_form.addOption('topic', options[i], options[i]);
}
g_form.setValue('topic', options[0]);
}
Update your OnChange Catalog Client Script for category field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'Leadership') {
loadTopicOptionsLeadership();
} else {
g_form.clearOptions('topic');
g_form.addOption('topic', '', '-- None --');
}
}
This ensures that:
- On form load, if "Leadership" is selected, topic options populate.
- When the user changes the category, the topic field updates accordingly.
- You don’t repeat the same topic logic in multiple places.
Thanks and Regards,
Chiranjeevi R
Please mark as Correct Answer/Helpful, if applicable.
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB
Please mark as Correct Answer/Helpful, if applicable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2025 10:08 AM
Hi @tomaslindev
Your current OnLoad script always calls loadTopicOptionsLeadership(), regardless of the actual value in the "category" field. This is likely why you're not seeing the correct behavior when a user changes the category.
please follow steps to improve your setup
Step 1: Update loadTopicOptionsLeadership() to be reusable.
Your function looks good — we’ll just make it modular so you can call it from multiple places (OnLoad + OnChange).
Step 2: Add logic to only call loadTopicOptionsLeadership() if the selected category is "Leadership
onLoad script:
function onLoad() {
var category = g_form.getValue('category');
if (category == 'Leadership') {
loadTopicOptionsLeadership();
}
}
loadTopicOptionsLeadership() function (unchanged, just reusable):
function loadTopicOptionsLeadership() {
g_form.clearOptions('topic');
var options = [
'Leadership escalation',
'Leadership recommendation',
'EHS Policy',
'Organization roles, responsibilities and authorities',
'Leadership Participation/Consultation request'
];
g_form.addOption('topic', '', '-- None --');
for (var i = 0; i < options.length; i++) {
g_form.addOption('topic', options[i], options[i]);
}
g_form.setValue('topic', options[0]);
}
Update your OnChange Catalog Client Script for category field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'Leadership') {
loadTopicOptionsLeadership();
} else {
g_form.clearOptions('topic');
g_form.addOption('topic', '', '-- None --');
}
}
This ensures that:
- On form load, if "Leadership" is selected, topic options populate.
- When the user changes the category, the topic field updates accordingly.
- You don’t repeat the same topic logic in multiple places.
Thanks and Regards,
Chiranjeevi R
Please mark as Correct Answer/Helpful, if applicable.
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB
Please mark as Correct Answer/Helpful, if applicable.