onChange script error: TypeError: Cannot read property 'ToString' of null function () { [native code] }

Krishna92
ServiceNow Employee
ServiceNow Employee

Hi Community,

I have implement below script for my requirement when it global scope its working and custom scope script not working getting error is"onChange script error: TypeError: Cannot read property 'ToString' of null function () { [native code] }"

could you please assist into this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

 

 

    var encstr = g_form.getValue('data_required');

 

 

    if (encstr.indexOf('-') >= 0) {

        alert('Please select a positive number -> A negative number is not valid filter criteria. Please update your filter criteria and try again.');

 

 

        g_form.clearValue('data_required');

    }

 

 

 

 

    var ga = new GlideAjax('sn_dart_access.CounttheDARTInstances');

    ga.addParam('sysparm_name', 'getTheCount');

    ga.addParam('sysparm_encrstr', encstr);

    ga.getXMLWait();

 

 

 

    var count = ga.getAnswer();

 

    g_form.clearOptions('instances_required');

    if (count == 0) {

        g_form.clearValue('data_required');

        g_form.showFieldMsg('data_required', 'The filters you have selected do not match any instances we can provide. Please modify in order to find some valid instances', 'error');

 

        return;

    }

    var ga2 = new GlideAjax('global.getTheInstancesCount');

    ga2.addParam('sysparm_name', 'getTheCount');

    ga2.addParam('sysparm_count', count);

    ga2.getXMLWait();

    var choicearr = ga2.getAnswer();

    choicearr = choicearr.ToString('').split(',');

    g_form.clearOptions('instances_required');

    g_form.addOption('instances_required', '', '--None--');

    for (var i = 0; i < choicearr.length; i++) {

        g_form.addOption('instances_required', choicearr[i], choicearr[i]);

    }

 

}

Many thanks,
Krishna

 

10 REPLIES 10

Daniel Draes
ServiceNow Employee
ServiceNow Employee

I would think your error is this line:

 

    choicearr = choicearr.ToString('').split(',');

Not knowing at the top of my head what ga2.getAnswer() will return as an object type I would still expect a toString-method to be lower case t vs upperCase T.

 

 

Krishna92
ServiceNow Employee
ServiceNow Employee

Hi Daniel,

Thanks for reply!

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

 

var encstr = g_form.getValue('data_required');


if (encstr.indexOf('-') >= 0) {
alert('Please select a positive number -> A negative number is not valid filter criteria. Please update your filter criteria and try again.');


g_form.clearValue('data_required');
}

 


var ga = new GlideAjax('sn_dart_access.CounttheDARTInstances');
ga.addParam('sysparm_name', 'getTheCount');
ga.addParam('sysparm_encrstr', encstr);
ga.getXMLWait();

 

var count = ga.getAnswer();

g_form.clearOptions('instances_required');
if (count == 0) {
g_form.clearValue('data_required');
g_form.showFieldMsg('data_required', 'The filters you have selected do not match any instances we can provide. Please modify in order to find some valid instances', 'error');

return;
}
var ga2 = new GlideAjax('global.getTheInstancesCount');
ga2.addParam('sysparm_name', 'getTheCount');
ga2.addParam('sysparm_count', count);
ga2.getXMLWait();
var choicearr = ga2.getAnswer();
alert(choicearr);
choicearr = choicearr.toString().split(',');
g_form.clearOptions('instances_required');
g_form.addOption('instances_required', '', '--None--');
for (var i = 0; i < choicearr.length; i++) {
g_form.addOption('instances_required', choicearr[i], choicearr[i]);
}



}

Still getting error and getting choicearr is null values.

Thanks

Krishna

Ok, at least we have isolated the line causing an issue 😄

You mention it works fine from global scope but not from a scoped application, correct?

Does your scoped app have a cross-scope-privilege for the called api? This should be GlideAjax as well as getTheInstancesCount.

I am also realizing you have the GlideAjax calls in there....

var ga = new GlideAjax('sn_dart_access.CounttheDARTInstances');

var ga2 = new GlideAjax('global.getTheInstancesCount');

 

Are you saying the first one works but the second not?

Maybe I am missing a point here 😕