- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 02:05 PM
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;
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 02:32 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 02:12 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 08:08 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2019 08:15 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2019 02:20 PM
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'
};