List Collector with multiple values calling a script with OnChange client script.

Monique Assaf
Tera Contributor

Hi,

I am trying to confirm when a user selects a group of users with a list collector it calls a script include to check if the user requested is in the group selected. Currently I have this working for a single user, but when multiple users are added it doesn't return the correct result.

I also would like remove the user that is currently in the group out of the list.

Any assistance is greatly appreciated. 

Catalog client script:

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

    //Checks the users in the selected group.
    {
        var group = g_form.getValue('add_select_the_group');
        var user = 'select_users';
        var selectedusers = g_form.getValue(user).split(',');
        var ga = new GlideAjax('LookupUserGroups');
        ga.addParam('sysparm_name', 'getUserGroups');
        ga.addParam('sysparm_user_id', selectedusers);
        ga.addParam('sysparm_group_id', group);
        ga.getXML(ajaxcall);

    }

    function ajaxcall(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert(answer);
        alert('The requested user is currently a member of the selected group.');

    }
}

Script Includes:

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

    getUserGroups: function() {
        var user_id = this.getParameter('sysparm_user_id');
        var pagroup = this.getParameter('sysparm_group_id');
        var groups_ids = '';
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', user_id);
        gr.addQuery('group', pagroup);
        gr.query();
        if (gr.next()) {
            return true;

        } else {
            return false;
        }
    },
    type: 'LookupUserGroups'
});

1 ACCEPTED SOLUTION

Hi,

1) you will have to determine which one is the latest value by comparing oldValue and newValue

2) newValue will hold 1 sysId extra than oldValue

use this -> to check for all users at once

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

	getUserGroups: function() {
		var user_id = this.getParameter('sysparm_user_id');
		var pagroup = this.getParameter('sysparm_group_id');
		var userCount = user_id.split(',').length;

		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', 'IN', user_id);
		gr.addQuery('group', pagroup);
		gr.query();
		var foundCount = gr.getRowCount();

		if(userCount == foundCount)
			return true; // it means all users are part of group selected
		else
			return false; // it means any 1 user is not part of group selected

	},
	type: 'LookupUserGroups'
});

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

then why not apply advanced ref qualifier on that list collector and only show members of the group selected.

here is the approach

Ensure in variable attributes add this

ref_qual_elements=add_select_the_group

javascript: 'sys_idIN' + new LookupUserGroups().getUserGroups(current.variables.add_select_the_group);

Script Include:

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

	getUserGroups: function(group) {
		var arr = [];
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('group', pagroup);
		gr.query();
		while (gr.next()) {
			arr.push(gr.getValue('user'));	
		} 
		return arr.toString();
	},
	
	type: 'LookupUserGroups'
});

regards
Ankur

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

Hi Ankar,

Thank you for your reply.

I would like this list collector in the catalog item to list all users so that the requestor can add users to this group. If the user they select is currently in the group I would like an error to be displayed and then removed from that list. They should only be able to add users to the list that are not currently in the list.

Hope this clarifies my requirements.

Thanks
Monique

Hi,

then update script as this

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

	getUserGroups: function() {
		var user_id = this.getParameter('sysparm_user_id');
		var pagroup = this.getParameter('sysparm_group_id');
		var groups_ids = '';
		var foundCount = 0;

		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', 'IN', user_id);
		gr.addQuery('group', pagroup);
		gr.query();
		var totalCount = gr.getRowCount();
		while(gr.next()){
			foundCount++;
		}
		
		if(totalCount == foundCount)
			return true;
		else
			return false;
		
	},
	type: 'LookupUserGroups'
});

regards
Ankur

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

Hi Ankur,

Thank you for your response.

This does not provide the correct result.

e.g. if I select 3 users in a list every time I enter a name it should call the script include to confirm if those users individually are in the group selected. So the 'user_id' could be a single name or comma separated list with multiple values.

Thanks
Monique