I have a script include that is quering a user and returning all of the assignment groups the user is in. I am only getting one group being returned when there are supposed to be several.

smetsch
Kilo Contributor

I have a script include that is querying a user and returning all of the assignment groups that the user is in. I am only getting  one group being returned when there are supposed to be several. Any thoughts?  Here is my script include:

function getUsersGroups(){
var answer = '';
var user = current.variables.install_network_device;
var grmembers = new GlideRecord('sys_user_grmember');
grmembers.addQuery('user', user);
grmembers.query();
while (grmembers.next()){
if (answer.length > 0){
answer += (',' + grmembers.group.sys_id);
} else {
answer = grmembers.group.sys_id;
}
}
return 'sys_idIN' + answer;
}

1 ACCEPTED SOLUTION

SwarnadeepNandy
Mega Sage

Hi,

Please try the below code.

function getUsersGroups() {
    var answer = [];
    var user = current.variables.install_network_device;
    var grmembers = new GlideRecord('sys_user_grmember');
    grmembers.addQuery('user', user);
    grmembers.query();
    while (grmembers.next()) {
        answer.push(grmembers.group.toString());
    }
    return 'sys_idIN' + answer;
}

Regards,

Swarnadeep Nandy

View solution in original post

9 REPLIES 9

Allen Andreas
Administrator
Administrator

Hi,

You don't have an array set, so var answer = [];

Then you can remove the answer.length as that doesn't apply here...

then in the result, you're not pushing the answer to the array, like so: answer.push(grmembers.group+',');

then in the return, you could try return 'sys_idIN'+answer.toString();

since script includes only return string.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

There's a reply to this, but I can't see it for some reason...maybe that's a good thing? lol...anyways...amazed by all the responses and variations here...

Good luck!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Good Morning,

I see you've marked a correct answer. I'm glad you found something that works for you. Seems I said basically the same thing? Just...didn't write the actual lines for you and do all the work, but instead gave you each correction and explained why...

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Laurie Marlowe
Kilo Explorer

Try this:

var BackfillAssignmentGroup = Class.create();
BackfillAssignmentGroup.prototype = {
	initialize: function() {
	},
 
	BackfillAssignmentGroup:function() {
		var gp = ' ';
		var a = current.assigned_to;
 
		//return everything if the assigned_to value is empty
		if(!a)
			return;
		//sys_user_grmember has the user to group relationship
		var grp = new GlideRecord('sys_user_grmember');
		grp.addQuery('user',a);
		grp.query();
		while(grp.next()) {
			if (gp.length > 0) {
				//build a comma separated string of groups if there is more than one
				gp += (',' + grp.group);
			}
			else {
				gp = grp.group;
			}
		}
		// return Groups where assigned to is in those groups we use IN for lists
		return 'sys_idIN' + gp;
	},
	type: 'BackfillAssignmentGroup'
};