Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to populate list collector using Client Script and Glide Ajax

Community Alums
Not applicable

HI Team,

 

I have a requirement wherein I am doing GlideAjax and trying to populate listCollector based on OnChange Client Script.

But below script in line no 18 is not working

 

Ankur20_0-1717166723662.png

 

1 ACCEPTED SOLUTION

Hi @Community Alums 

 

I have done the same requirement in PDI sharing the code:

Client Script:

function onLoad() {
    var requestedFor = g_form.getValue('opened_by');
	alert(requestedFor);

    var ga = new GlideAjax('GetManagerDetailsScript');
    ga.addParam('sysparm_name', 'getRoleDetails');
    ga.addParam('sysparm_requested_for', requestedFor);
    ga.getXMLAnswer(callback);
	alert("into callback");

    function callback(response) {
        var answer = response;
		alert(answer);
        var roles = answer.split(',');
        alert(roles);
		var accessoriesValues = [];

        for (var i = 0; i < roles.length; i++) {
            accessoriesValues.push(roles[i]);
        }


        g_form.setValue('accessories_1', accessoriesValues);
    }
}

and Script include :

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

    getRoleDetails: function() {
        var roles = [];
        var requestedFor = this.getParameter('sysparm_requested_for');
        var roleuser = new GlideRecord('sys_user_has_role');
        roleuser.addQuery('user', requestedFor);
        roleuser.query();
        while (roleuser.next()) {
            roles.push(roleuser.role.name);
        }
        return roles.join(',')
    },

});

 

Thanks and Regards

Sai Venkatesh

View solution in original post

4 REPLIES 4

SAI VENKATESH
Kilo Patron
Kilo Patron

Hi @Community Alums 

 

Can you also please share the server Script?

Hi @Community Alums 

 

I have done the same requirement in PDI sharing the code:

Client Script:

function onLoad() {
    var requestedFor = g_form.getValue('opened_by');
	alert(requestedFor);

    var ga = new GlideAjax('GetManagerDetailsScript');
    ga.addParam('sysparm_name', 'getRoleDetails');
    ga.addParam('sysparm_requested_for', requestedFor);
    ga.getXMLAnswer(callback);
	alert("into callback");

    function callback(response) {
        var answer = response;
		alert(answer);
        var roles = answer.split(',');
        alert(roles);
		var accessoriesValues = [];

        for (var i = 0; i < roles.length; i++) {
            accessoriesValues.push(roles[i]);
        }


        g_form.setValue('accessories_1', accessoriesValues);
    }
}

and Script include :

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

    getRoleDetails: function() {
        var roles = [];
        var requestedFor = this.getParameter('sysparm_requested_for');
        var roleuser = new GlideRecord('sys_user_has_role');
        roleuser.addQuery('user', requestedFor);
        roleuser.query();
        while (roleuser.next()) {
            roles.push(roleuser.role.name);
        }
        return roles.join(',')
    },

});

 

Thanks and Regards

Sai Venkatesh

Harsh Vardhan
Giga Patron

@Community Alums  There is one typo in your script, line number 18. You have used + instead ,  (comma)

 

g_form.setValue('u_accounts', acc[i]);

 

 

swathisarang98
Giga Sage

Hi @Community Alums ,

 

what are you getting in line number 14 ?

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang