Script include is not being called from a Catalog Client Script

Fotios Kossyvas
Tera Contributor

I have these two fields in a form on Service Portal

FotiosKossyvas_0-1738826954538.png

The u_utente_da_copiare is a Reference field to the sys_user and the u_gruppi_da_copiare is a List Collector connected to the sys_user_group

All i want is getting the groups of the user selected on u_utente_da_copiare into u_gruppi_da_copiare.

So, i created this Script Include, callable from Client and from all scopes

FotiosKossyvas_2-1738827221621.png

Code:

 

var CDP_UserGroup = Class.create();
CDP_UserGroup.prototype = {
    initialize: function() {},

    getGroups: function() {
        var userSysID = this.getParameter('sysparm_userSysID');
        var groups = [];
        var gr = new GlideRecord('sys_user_grmember'); 
        gr.addQuery('user', userSysID);
        gr.query();

        while (gr.next()) {
            groups.push(gr.group.toString()); 
        }
        return groups.join(',');
    },

    type: 'CDP_UserGroup'
};

 

 I created this ACL, in order to enable a user with itil role to call it

FotiosKossyvas_3-1738827349604.png

and finally i created this Catalog Client Script in order to call the Script include

FotiosKossyvas_4-1738827480598.png

Code:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('CDP_UserGroup');
    ga.addParam('sysparm_name', 'getGroups'); 
    ga.addParam('sysparm_userSysID', newValue);
    ga.getXMLAnswer(updateGroups);

    function updateGroups(answer) {
        if (answer) {
            g_form.setValue('u_gruppi_da_copiare', response);
        } else {
            g_form.setValue('u_gruppi_da_copiare', '0bce384a1b517410e8b363d3b24bcb81'); //for debug reasons
        }
    }

}

 

I only get as result the line marked as "for debug reasons".

I had put alert() to debug and i saw that the newValue (used to call the Script Include) had a valid value and running the script by hand is correctly returns groups id's separated by comma (which is just what i want).

FotiosKossyvas_5-1738828216593.png

 

I think i followed all the suggestions i found in the Servicenow documentation and this forum (I've read a lot of posts)  but i don't understand why the aswer variable i'm getting in return is still null (i've checked with an alert(answer)).

Could you please help me?

Fotios

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Fotios Kossyvas 

your script include syntax for client callable is wront

It should be this way

 

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

    getGroups: function() {
        var userSysID = this.getParameter('sysparm_userSysID');
        var groups = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userSysID);
        gr.query();
        while (gr.next()) {
            groups.push(gr.group.toString());
        }

        if (groups.length > 0)
            return groups.join(',');
        else
            return '';
    },

    type: 'CDP_UserGroup'
});

 

AnkurBawiskar_1-1738829756652.png

 

 

Client script will be this

 

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

    if (newValue == '')
        g_form.clearValue('u_gruppi_da_copiare');

    var ga = new GlideAjax('CDP_UserGroup');
    ga.addParam('sysparm_name', 'getGroups');
    ga.addParam('sysparm_userSysID', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            g_form.setValue('u_gruppi_da_copiare', answer.toString());
        } else {
            g_form.setValue('u_gruppi_da_copiare', '0bce384a1b517410e8b363d3b24bcb81'); //for debug reasons
        }
    });

}

 

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Fotios Kossyvas 

your script include syntax for client callable is wront

It should be this way

 

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

    getGroups: function() {
        var userSysID = this.getParameter('sysparm_userSysID');
        var groups = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userSysID);
        gr.query();
        while (gr.next()) {
            groups.push(gr.group.toString());
        }

        if (groups.length > 0)
            return groups.join(',');
        else
            return '';
    },

    type: 'CDP_UserGroup'
});

 

AnkurBawiskar_1-1738829756652.png

 

 

Client script will be this

 

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

    if (newValue == '')
        g_form.clearValue('u_gruppi_da_copiare');

    var ga = new GlideAjax('CDP_UserGroup');
    ga.addParam('sysparm_name', 'getGroups');
    ga.addParam('sysparm_userSysID', newValue);
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            g_form.setValue('u_gruppi_da_copiare', answer.toString());
        } else {
            g_form.setValue('u_gruppi_da_copiare', '0bce384a1b517410e8b363d3b24bcb81'); //for debug reasons
        }
    });

}

 

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

Thank you @Ankur Bawiskar ! Your solution was well described and correct 😀