User field should show users depending on the selection group selected in group field

anushadande1793
Tera Contributor

Hi 

 

I had requirement to allow the user to select multiple users (list collector type) on user field and users should be populated depending on the group selected on group field referring to sys_user_group table. Can you please help me how to achieve this. I have given a trail calling script include from user field and writing onchange client script but not working . I have given below reference qualifier as well by referring user field to group member table but created field is showing up as it has display value as true . I tried removing this dispaly value and adding other fields to show up using variable attributes but no luck, display value is showing up 

javascript&colon;"group="+current.variables.<group_variable_name>;

 

Can someone help on the script part how to achieve this

 

Thank you,

Anusha

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@anushadande1793 

you should be using advanced ref qualifier on the list collector variable

Script Include:

var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getMembers: function(groupId){
		var id = [];
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery("group", groupId);
		gr.query();
		while(gr.next()){
			id.push(gr.getValue("user"));
		}
		return "sys_idIN"+id.join();
	},

    type: 'UserUtils'
});

Also use this in the variable attributes for that list collector; without this it won't work in list collector variable

ref_qual_elements=groupVariableName

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

Hi Ankur,

It is not working as expected. Instead group members it is showing all users from user table.

I have given as user field as list collector and written given script include and called this script include from reference qualifier like below 

javascript&colon;new PopulateUser().getUsers(u_group_name);

But no luck. I have simply tried using refernce qualifier by keeping user field to group member table but it is giving created date as display value of created date  is true. But I don't want to change this as it is global change. Can you please help me with the solution

 

Thanks

Anusha

chandu335
Tera Contributor

Hello,

I had similar requirement which I was trying to practice in my PDI. I used user removal field as List collector to sys_user table and the group selection field as reference to sys_user_group table so when the group is selected the user removal should show only the users are part of this group.

You should write script  include which should be called from Catalog via Reference qualifier syntax "javascript&colon; new scriptincludename().function(current);, the current record is passed inside the function to script include. 

The Script  is as below:

var CatalogGroupsfilter = Class.create();
CatalogGroupsfilter.prototype = Object.extendsObject(AbstractAjaxProcessor, {
        getusers: function(current) {
        var grp = current;  
        gs.log('1. The group sys_id is :' + grp.variables.group_selection);
        var grps = [];     
        var crazy = new GlideRecord('sys_user_grmember');
        crazy.addQuery('group.sys_id', grp.variables.group_selection);
        crazy.query();
        gs.log('2. The group has this many users  :' + crazy.getRowCount());
        while (crazy.next()) {
        grps.push(crazy.user.sys_id);
        }
        var inc = grps.toString();
        gs.log('3. The users from this filter are :' + inc);
        return 'sys_idIN'+inc;
        },
      type: 'CatalogGroupsfilter'  
    });
The catch here is when you load the new catalog form the group selection field will be empty so I could not able to be see any users so once after selecting the group and clicking the filter of user field then I could able to see the users who are part of the group.
If it's helped you or others in any way mark as helpful.