Trying to call script include from reference qualifier but not getting the correct groups in the catalog item

Afsar Hussain
Kilo Guru

Hi,

I have requirement that there is one catalog item with two variables User and Group. User is of single line text variable and group is reference type variable.

When I click on group search icon, it should display list of only those groups of which currently logged in user is a part but I am getting all groups in the list.

I have created script include for this and calling this script include in the reference qualifier field of group variable of the Catalog item.  

I have attached the required screenshots.

Pls help me to find what is wrong with my code.

Below is the script include I have written-

var GetUserGroups = Class.create();
GetUserGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroups:function(){
var arr=[];
var grp=new GlideRecord('sys_user_group');
grp.addQuery('active',true);
grp.query();
while(grp.next())
{
if(gs.getUser().isMemberOf(grp.name)== true)
{

arr.push(grp.name);
}

}

return arr;

},

type: 'GetUserGroups'
});

 

Regards,

Afsar

1 ACCEPTED SOLUTION

Hi @Afsar 

 

Finally, this will solve your issue:


Update Script Include:

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

	getMembers: function(userId) {
		var groups = [];
		var getMyGroup = new GlideRecord('sys_user_grmember');
		getMyGroup.addEncodedQuery('user.user_name=' + userId + '');
		getMyGroup.query();
		while(getMyGroup.next()) {
			groups.push(getMyGroup.getValue('group'));
		}
		
		//Added log to see what data is generated
		//gs.log('[DEBUG] Groups for User ID: ' + userId + '\n' + groups);
		if(JSUtil.notNil(groups)) {
			return groups.join(',');
		} else
			return '-1';
	},
	
    type: 'GetGroupMembers'
});

Reference Qualifier:

javascript: 'sys_idIN' + new GetGroupMembers().getMembers(current.variables.<backend_name_of_user_variable>);

 

Check the screenshot as well:

find_real_file.png

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

View solution in original post

17 REPLIES 17

Ankur Bawiskar
Tera Patron
Tera Patron

@Afsar 

when form loads you want groups of logged in user?

then when user changes you want groups of that selected user?

Regards
Ankur

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

Yes Ankur.

Any idea on this ??

Hi,

any reason why user variable is a string and not reference?

Regards
Ankur

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

Because user wants to remove himself for that purpose that catalog item has been designed.