Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Script not running in native UI

SHALIKAS
Tera Guru

I have written an onChange client script on server_name variable which is a list collector variable. This script is working in portal, but not in native UI

In native UI even the alert is not coming

 

function onChange(control, oldValue, newValue, isLoading) {
 
    if (isLoading || !newValue) {
        return;
    }

    // Get first selected server from list collector
    var firstServer = newValue.split(',')[0];
alert("firest"+firstServer);
    var ga = new GlideAjax('Coned_fetch_server_details');
    ga.addParam('sysparm_name', 'getServerDetails');
    ga.addParam('sysparm_sys_id', firstServer);

    ga.getXMLAnswer(function(answer) {
alert("Answer = [" + answer + "]");
        if (answer == 'Ansible') {
            g_form.setValue('task_for_automation', 'Yes');
        } else {
            g_form.setValue('task_for_automation', 'No');
        }

    });
}
The script include is
var Coned_fetch_server_details = Class.create();
Coned_fetch_server_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getServerDetails: function() {

        var serverId = this.getParameter('sysparm_sys_id');

        gs.info('Server Id = ' + serverId);

        var gr = new GlideRecord('cmdb_ci_server');

        if (gr.get(serverId)) {

            gs.info('Build Source = ' + gr.u_build_source);

            return gr.u_build_source.toString();
        }

        return '';
    },

    type: 'Coned_fetch_server_details'
});

 

5 REPLIES 5

HarishKumar6668
Kilo Sage

Hi @SHALIKAS ,

Please make sure the following configurations are set in the onChange Client Script:

  • UI Type: Select All
  • Applies on Requested Items: Checked
  • Applies on Catalog Tasks: Checked

    Screenshot 2026-09-02 at 5.51.12 PM.png

This will ensure that the onChange Client Script is applied appropriately when working with both Requested Items and Catalog Tasks in the Native UI.

If you find this response useful, please mark it as Helpful and Accept the Solution.

Regards,
Harish

Have already marked all the above things

Risto D_od_o
Tera Contributor

Just tried your code, try troubleshooting it with g_form.addInfoMessage("Answer = [" + answer + "]") instead of alert("Answer = [" + answer + "]"), from there you can at least see whether you are getting the right values.

Sujit Jadhav
Tera Guru

Hello @SHALIKAS ,

Try this updated script 

function onChange(control, oldValue, newValue, isLoading) {
// Stop execution on form initialization or if the field is empty
if (isLoading || !newValue) {
return;
}

// Convert newValue to string to prevent native UI object errors
var valueStr = String(newValue);
if (!valueStr) {
return;
}

// Extract the first Sys ID from the list collector string
var serverList = valueStr.split(',');
var firstServer = serverList[0] ? serverList[0].trim() : '';

if (!firstServer) {
return;
}

// Process the asynchronous GlideAjax request
var ga = new GlideAjax('Coned_fetch_server_details');
ga.addParam('sysparm_name', 'getServerDetails');
ga.addParam('sysparm_sys_id', firstServer);
ga.getXMLAnswer(function(answer) {
if (answer === 'Ansible') {
g_form.setValue('task_for_automation', 'Yes');
} else {
g_form.setValue('task_for_automation', 'No');
}
});
}