- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 01:51 PM - edited 01-10-2023 02:21 PM
Hi all,
I am working on automation to add/ remove members from assignments groups and I kind of stuck. Currently, I have this script included which gets only the member of a selected group when is to remove a user. Which works fine, it only shows me users that belong to the group.
function u_getMembers(groupSysId) {
var userRec = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
while (gr.next()) {
userRec.push(gr.getValue('user'));
}
userIds = userRec.join(',');
return "sys_idIN" + userIds;
}
I wanted to do the same when they select add a user to the group, I wanted to show only users that doesn't belong to the group selected.
The way I am calling the script include is on the variable I am using this: javascript:u_getMembers(current.variables.assignment_group);
So far this is what I have, but not working
function u_getMembers(groupSysId) {
var nonMemberIds = [];
var grMembers = new GlideRecord('sys_user_grmember');
grMembers.addQuery('group', groupSysId);
grMembers.query();
var grUsers = new GlideRecord('sys_user');
grUsers.query();
while (grUsers.next()) {
var isMember = false;
while (grMembers.next()) {
if (grUsers.getValue('sys_id') == grMembers.getValue('user')) {
isMember = true;
break;
}
}
if (!isMember) {
nonMemberIds.push(grUsers.getValue('user'));
}
grMembers.rewind();
}
userIds = nonMemberIds.join(',');
return "sys_idIN" + userIds;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 02:08 PM
Thanks to all that replay @Prateek kumar and @Tony Chatfield1 I was able to fix my issue. It was a simple change in my original code. I am posting in here for reference and future developers. Instead of use return "sys_idIN" I need to user "sys_idNOT IN"
function u_getMembers(groupSysId) {
var userRec = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
while (gr.next()) {
userRec.push(gr.getValue('user'));
}
userIds = userRec.join(',');
// instead of using ''sys_idIN'' I needed to user "sys_idNOT IN"
return "sys_idNOT IN" + userIds;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 02:18 PM - edited 01-10-2023 02:20 PM
Hi, normally I would expect the ref qualifier configuration to look sometime like this
- assuming the field 'u_getMembers' a list of sys_id's
javascript: 'u_getMembersIN' + new scriptIncludeName().functionName(parameters);
I would recommend that you add some debugging\logging to your code to first confirm the function is called correctly and then to confirm the encoded query that you are returning.
Once you have this functional you could pass multiple parameters and combine the exclusion requirement into the current function, or call a second function and append to your current results.
But beware of browser limitations and potential performance impact if your resulting query is very large (long)
Also, exclusion queries across multiple different tables IE sys_user and sys_user_grmember may not be simple to achieve.
Edit:
Not the script should be javascriptcolon but the platform appends text and a semicolon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 03:40 PM
How about below:
function u_getMembers(groupSysId) {
var nonMemberIds = [];
var memberIds = [];
var grMembers = new GlideRecord('sys_user_grmember');
grMembers.addQuery('group', groupSysId);
grMembers.query();
while(grMembers.next()){
memberIds.push(grMembers.sys_id.toString());
}
var grUsers = new GlideRecord('sys_user');
grUsers.query();
while (grUsers.next()) {
nonMemberIds.push(grUsers.sys_id.toString());
}
var finalArray = [];
var arrayUtil = new ArrayUtil();
finalArray = arrayUtil(nonMemberIds, memberIds);
return "sys_idIN" + finalArray;
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2023 08:25 AM
Hi @Prateek kumar , thanks for your reply but still returning users that belongs to the group. For example, this is the option when I select Members to remove, it only presents users that belongs to the group selected
Using your code in the Add Member section still return members that belongs to the group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 02:08 PM
Thanks to all that replay @Prateek kumar and @Tony Chatfield1 I was able to fix my issue. It was a simple change in my original code. I am posting in here for reference and future developers. Instead of use return "sys_idIN" I need to user "sys_idNOT IN"
function u_getMembers(groupSysId) {
var userRec = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
while (gr.next()) {
userRec.push(gr.getValue('user'));
}
userIds = userRec.join(',');
// instead of using ''sys_idIN'' I needed to user "sys_idNOT IN"
return "sys_idNOT IN" + userIds;
}