- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2023 07:28 PM - edited 02-13-2023 07:30 PM
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'
};
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2023 03:24 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2023 04:40 AM
Thank u @-Andrew- for this solution. It is working fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2023 05:27 AM
Hello,
You can use advance qualifier in caller dictionary field and in that call a script include.
Advance qualifier code = javascript:new global.Callerid_sn().getmembers();
script include code