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 from client script

rahul gupta6
Tera Contributor

Hi,

How can we add managers in list collector variable based on group selected in another list collector variable using onSubmit client script.

Kindly note that multiple groups may have same managers but the list collector variable holding managers should contain unique managers.

6 REPLIES 6

GlideFather
Tera Patron

Hi @rahul gupta6,

 

it will require a client script and glideAjax (script include).

 

If you mentioned onSubmit, the list collector field will be empty until it's submitted, and then it will populate it, wouldn't it be easier to do that without CS or with onLoad or onChange one?

 

Also, it this happening in portal or backend? based on that it's either client script, catalog client script or both...

 

please share what you tried and what error do you get to help you debug

_____
This reply is 100 % GlideFather and 0 % AI

It is in portal. Have written a client script with g_form.getValue("groups") and passing in a script include. The script include do the gliderecord on user table and get the manager sys id and store it in string variable (storing it in an array was causing issue - in the client script the sysids were not available).

 

Then taking the sysids (in string format) spliting in comma separated and for each value again calling script include and trying to pass the individual manager sys id back to client script and adding each individual sysid in a string variable in client script and then doing f_form.setValue("list_collector_manager", sysids) but it is not working.

 

Tried getXML, getXMLAnswer, JSON.stringify, JSON.parse etc but unable to get it done.

@rahul gupta6 

in single GlideAjax this is possible

This should work for you

please enhance it

Script Include: It should be client callable

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

    getUserGroups: function() {

        var managerArr = [];
        var groups = this.getParameter('sysparm_groups');
        var recGrp = new GlideRecord('sys_user_grmember');
        recGrp.addQuery('group.sys_id', 'IN', groups);
        recGrp.query();
        while (recGrp.next()) {
            managerArr.push(recGrp.manager.toString());
        }
        var arrayUtil = new global.ArrayUtil();
        managerArr = arrayUtil.unique(managerArr);
        return managerArr.toString();
    },

    type: 'getAssignGroups'
});

AnkurBawiskar_0-1764755667979.png

onChange catalog client script on 1st variable

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

    // give here the list collector variable name
    g_form.clearValue('2ndVariableName');

    var ga = new GlideAjax('getAssignGroups');
    ga.addParam('sysparm_name', "getUserGroups");
    ga.addParam('sysparm_groups', g_form.getValue('1stVariableName')); // give here 1st variable name
    ga.getXMLAnswer(function(answer) {
        if (answer != '') {
            g_form.setValue('2ndVariableName', answer); // give here user_id variable name
        }
    });

}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Thank you Ankur. This works fine when I select the first group. But on selecting the second group, the manager field gets cleared and nothing is populated.