I want to show group members in caller feild which are present in 2 groups.

ServicenowDev19
Tera Contributor

I have 2 groups A and B and in that 2 groups i Have added some users.So i want to get the users in caller feild which are present in both the groups.

I have writen the below code but it is showing me all the users of both the groups.

 

Script include:

var AandB = Class.create();
AandB.prototype = {
initialize: function() {},

getgrpMembers: function() {
var users = '';
//var users2 = '';
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', "d73ba2a44785ed5010937d18c26d43f9");
gr.query();

while (gr.next()) {
users += gr.user.sys_id + ",";

var gb = new GlideRecord('sys_user_grmember');
gb.addQuery('group', "5c8b62e047c5ed5010937d18c26d4313");
gb.query();

while (gb.next()) {
// users += gr.user.sys_id + ",";
users += gb.user.sys_id + ",";
}
}
return 'sys_idIN' + users.toString();

//return users;
},type: 'AandB'
};

 

1 ACCEPTED SOLUTION

-Andrew-
Kilo Sage

It looks like there are a couple of issues with the code you provided. The first issue is that you have nested while loops in your getgrpMembers() function, which means that you are iterating over all users in group A for every user in group B. This is likely why you are seeing all users from both groups.

To fix this, you should move the second while loop outside of the first one so that you only iterate over group B once. Here's an updated version of your script:

var AandB = Class.create();
AandB.prototype = {
initialize: function() {},
getgrpMembers: function() {
var usersA = [];
var usersB = [];
var usersAB = [];
var groupA = "d73ba2a44785ed5010937d18c26d43f9";
var groupB = "5c8b62e047c5ed5010937d18c26d4313";

 

// Query group A for members
var grA = new GlideRecord('sys_user_grmember');
grA.addQuery('group', groupA);
grA.query();

while (grA.next()) {
  usersA.push(grA.user.sys_id.toString());
}

// Query group B for members
var grB = new GlideRecord('sys_user_grmember');
grB.addQuery('group', groupB);
grB.query();

while (grB.next()) {
  usersB.push(grB.user.sys_id.toString());
}

// Find the intersection of the two user arrays
usersAB = usersA.filter(function(userId) {
  return usersB.indexOf(userId) > -1;
});

// Return the resulting string for the caller field query
return 'sys_idIN' + usersAB.join(',');

 

},
type: 'AandB'
};

You can then call the getgrpMembers() function from a client script or a business rule to set the value of the caller field.

View solution in original post

6 REPLIES 6

Abhay Kumar1
Giga Sage

@ServicenowDev19  you are not validation user before adding into users variable, if you check if user is matching then only add ,it should work.

I wi suggest you to better use below query:

@logivention You are just adding user into users variable but no validation to check if first group user matches then only add user into users variable,if you add this should work for you.

I have a better suggest for you ,if this works for you,.

var users = [];// to keep matching users
var agg = new GlideAggregate('sys_user_grmember');
agg.addAggregate('COUNT', 'user');
agg.orderBy('user');
agg.addQuery('group', 'sys_id_of_grp1').addOrCondition('group', 'sys_id_of_grp2');
agg.query();
while (agg.next()) {
var usercnt = agg.getAggregate('COUNT', 'user');
if (usercnt > 1) {
users.push(agg.getValue('user'));
}
}

And then finally return users array.

Hope this will help

 

Swapnil Pujari
Tera Contributor

Hello @ServicenowDev19 ,

 Can you please explain more about your issue?

-Andrew-
Kilo Sage

It looks like there are a couple of issues with the code you provided. The first issue is that you have nested while loops in your getgrpMembers() function, which means that you are iterating over all users in group A for every user in group B. This is likely why you are seeing all users from both groups.

To fix this, you should move the second while loop outside of the first one so that you only iterate over group B once. Here's an updated version of your script:

var AandB = Class.create();
AandB.prototype = {
initialize: function() {},
getgrpMembers: function() {
var usersA = [];
var usersB = [];
var usersAB = [];
var groupA = "d73ba2a44785ed5010937d18c26d43f9";
var groupB = "5c8b62e047c5ed5010937d18c26d4313";

 

// Query group A for members
var grA = new GlideRecord('sys_user_grmember');
grA.addQuery('group', groupA);
grA.query();

while (grA.next()) {
  usersA.push(grA.user.sys_id.toString());
}

// Query group B for members
var grB = new GlideRecord('sys_user_grmember');
grB.addQuery('group', groupB);
grB.query();

while (grB.next()) {
  usersB.push(grB.user.sys_id.toString());
}

// Find the intersection of the two user arrays
usersAB = usersA.filter(function(userId) {
  return usersB.indexOf(userId) > -1;
});

// Return the resulting string for the caller field query
return 'sys_idIN' + usersAB.join(',');

 

},
type: 'AandB'
};

You can then call the getgrpMembers() function from a client script or a business rule to set the value of the caller field.

Tank u @-Andrew-  for the solution.This is working for me.