We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

GlideAjax returning null from Script Include

NishmithaS
Giga Contributor

I'm trying to convert List Collector sys_ids to user names using GlideAjax. The List Collector returns comma-separated sys_ids, which I pass to a client-callable Script Include. However, the GlideAjax response is always null.

 

Script include 

var getwatchlistusernames = Class.create();
getwatchlistusernames.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getName: function() {
var ids = this.getParameter('sysparm_ids');
var names = [];

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', ids);
gr.query();

while (gr.next()) {
names.push(gr.getValue('name'));
}

return names.join(',');
},

type: 'getwatchlistusernames'
});

 

Catalog Client script 

var ga = new GlideAjax('getwatchlistusernames');

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

ga.addParam('sysparm_ids', g_form.getValue('user'));

 

ga.getXMLAnswer(callback);

 

function callback(response) {

    alert(response);

    // g_form.setValue('short_description', response);

}

1 ACCEPTED SOLUTION

Hi @NishmithaS ,

 

i looked into the your both script include and catalog client script. i have made one change in script include function name and it worked in my PDI

Script include:

var getwatchlistusernames = Class.create();
getwatchlistusernames.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getNames: function() { // getName changed to getNames
        var ids = this.getParameter('sysparm_ids');
        var names = [];

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', 'IN', ids);
        gr.query();

        while (gr.next()) {
            names.push(gr.getValue('name'));
        }

        return names.join(',');
    },

    type: 'getwatchlistusernames'
});

 

Catalog client script:

    var ga = new GlideAjax('getwatchlistusernames');
    ga.addParam('sysparm_name', 'getNames');// from getName to getNames
    ga.addParam('sysparm_ids', g_form.getValue('user'));
    ga.getXMLAnswer(callback);
    function callback(response) {

        alert(response);
		    // g_form.setValue('short_description', response);



    }

 

can you try this and let me know still your getting null as the resposne 

If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,
BK

View solution in original post

17 REPLIES 17

Tanushree Maiti
Tera Patron

Hi @NishmithaS 

 

Try this:

1. Script include

var getwatchlistusernames = Class.create();
getwatchlistusernames.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getName: function() {
var ids = this.getParameter('sysparm_ids');
if (!ids) {
return '';
}

var names = [];
var sysIdArray = ids.split(',');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', sysIdArray); //corrected here
gr.query();
while (gr.next())
{
names.push(gr.getValue('username')); //corrected here
}
return names.join(',');
},
type: 'getwatchlistusernames'
});

2. Catalog Client script

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

var ga = new GlideAjax('getwatchlistusernames');
ga.addParam('sysparm_name', 'getName');
ga.addParam('sysparm_ids', g_form.getValue('user'));
ga.getXMLAnswer(callback);
}
function callback(response) {
alert(response);
if (response) {
// g_form.setValue('short_description', response);
}
}

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

surajsengar
Giga Contributor

@NishmithaS Try Below Scripts

Script include:

var GetWatchlistUsernames = Class.create();
GetWatchlistUsernames.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getName: function() {
        var ids = this.getParameter('sysparm_ids');

 

        var names = [];
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', 'IN', ids);
        gr.query();

 

        while (gr.next()) {
            names.push(gr.getDisplayValue('name'));
        }

 

        return names.join(',');
    },

 

    type: 'GetWatchlistUsernames'
});



Client Script

var ga = new GlideAjax('GetWatchlistUsernames');
ga.addParam('sysparm_name', 'getName');
ga.addParam('sysparm_ids', g_form.getValue('user'));

 

ga.getXMLAnswer(function(answer) {
    alert(answer);
});

AnkurBaruah
Tera Contributor

Hi @NishmithaS 

 

1. Please ensure the client callable is checked.

2. ​If your Client Script is running in a scoped application (or Global) and the Script Include is in a different scope. ServiceNow will block the call unless:

​The Script Include's Accessible from field is set to All application scopes.

​You can add the name to your GlideAjax call (e.g., new GlideAjax('x_abc_scope.getwatchlistusernames')).

 

If my response helped mark as helpful and accept the solution.