Error Catalog Client Script OnLoad

tomaslindev
Mega Guru

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.

 

tomaslindev_1-1747658636459.png

 

 

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]);
}

 

1 ACCEPTED SOLUTION

ChiranjeeviR
Kilo Sage

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.

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.

View solution in original post

10 REPLIES 10

@tomaslindev 

I mentioned to remove the options you don't want but in the script you are adding

Please follow the steps I mentioned above.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Muhammad Salar
Giga Sage

Hello, didn't fully understand your requirement and your category field has value planning in your screenshot

hi @Muhammad Salar ,

When loading the form, this is what appears; I have been asked to display the values ​​related to the category that is loaded by default in the Topic field.

 

tomaslindev_0-1747661506213.png

 

Hello, 
working for me, i have used same script of yours

MuhammadAli1_0-1747664415298.png

I have used topic as select box type variable

is your issue resolved ?