Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Client script getOptions() issue (field choices not resetting when changing onChange field)

Bhojraj Chauhan
Tera Contributor

I have create an onChange client script on 'type' field with the logic to display choices in choice field 'suspension_reason'. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
  
    //Type appropriate comment here, and begin script below
    var internal1 = g_scratchpad.internalreasons;
    var external1 = g_scratchpad.externalreasons;
    var type1 = g_form.getValue('type');

    internal1 = internal1.split(',');
    external1 = external1.split(',');
    // Add the appropriate options based on the selected type
    if (type1 == 'Internal Suspend') {
        g_form.setValue('your_choice_field', '');
        for (var j1 = 0; j1 < external1.length; j1++) {
            g_form.removeOption('suspension_reason', external1[j1]);
        }
        for (var k1 = 0; k1 < internal1.length; k1++) {
            g_form.addOption('suspension_reason', internal1[k1]);
        }
    } else if (type1 == 'External Suspend') {
        g_form.setValue('your_choice_field', '');
        for (var l1 = 0; l1 < internal1.length; l1++) {
            g_form.removeOption('suspension_reason', internal1[l1]);
        }
        for (var m1 = 0; m1 < external1.length; m1++) {
            g_form.addOption('suspension_reason', external1[m1]);
        }
    }
    g_form.clearValue('suspension_reason');
}


Here, 

When I select type as Internal Suspend, the choices in suspension_reason fields show correctly. But when I change the type from Internal Suspend to External Suspend, the choices show 'None'. 
Same happens vice versa, when I select External Suspend, the choices in suspension_reason show properly, but when I reselect the type field as Internal Suspend, the choices show 'None'. 
What changes can I make to make it work as expected.
I have tried adding  g_form.clearOptions('suspension_reason'); before if/else if conditions doesnt work. 
Please drop if you guys have any suggestions to make.


 

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Bhojraj Chauhan 

try this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // Delay ensures dropdown is ready before modifying
    setTimeout(function() {
        var internal1 = g_scratchpad.internalreasons.split(',');
        var external1 = g_scratchpad.externalreasons.split(',');

        g_form.clearOptions('suspension_reason');
        g_form.addOption('suspension_reason', '', '-- None --', 0);

        if (newValue == 'Internal Suspend') {
            internal1.forEach(function(choice) {
                g_form.addOption('suspension_reason', choice, choice);
            });
        } else if (newValue == 'External Suspend') {
            external1.forEach(function(choice) {
                g_form.addOption('suspension_reason', choice, choice);
            });
        }

        g_form.clearValue('suspension_reason');
    }, 200);
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Ankur Bawiskar when I add clearOptions, the choice list returns as empty. Is there an alternative to use in place of clearOptions? That will fix the script.

 

BhojrajChauhan_0-1760601708306.png

 

@Bhojraj Chauhan 

try this

-> Remove options one-by-one

-> Explicitly manage the '-- None --' option

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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