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

Kartik Sethi
Tera Guru
Tera Guru

Hi @Afsar 

 

Could you please provide the Script Include in the question itself?

 

Thanks and regards,

Kartik

Added

Hi @Afsar 

Thanks for providing the script. Please find the updated script below:

Script Include:

var GetGroupMembers = Class.create();
GetGroupMembers.prototype = {
	
	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('sys_id'));
		}
		
		if(groups) {
			return groups.join(',');
		} else
			return '-1';
	},

    type: 'GetGroupMembers'
};

Screenshot:


find_real_file.png

You also need to have changes in your Reference Qualifier as provided below:

javascript: new GetGroupMembers().getMember(current.<variable_name>)

 


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

Hi @Afsar 

 

Are you still facing the issue?

If my answer solved your issue then please mark my answer as correct/helpful, so that other community members may get a solution if they face a similar issue!

Thanks and regards,

Kartik