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

Ankur Bawiskar
Tera Patron
Tera Patron

@tomaslindev 

category is defaulted to Leadership how?

why are you adding options when form loads?

let the options be as it is, simply set the topic when form loads to whatever value you want

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

Hi @Ankur Bawiskar 

It is set by default to the category variable. It is set by default to the category variable. Add options because I have been asked to.

 

tomaslindev_0-1747661387294.png

 

@tomaslindev 

Don't set it via Default value, do this

1) create onLoad catalog client script, set the default value as Leadership then use the same onLoad client script to remove the options you want and keep only the ones you need

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

hi@Ankur Bawiskar ,
I imagine this is what you're referring to? Unfortunately, it doesn't work.

function onLoad() {

    g_form.setValue('category', 'Leadership');
    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]);
}

tomaslindev_0-1747663186497.png