Add or remove member from the group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 03:42 AM
Hello All,
I have an requirement to Automate request for Add or remove member to group Item , we have two list collector fields
one is for Adding members and the other one for removing members , here my challenge is sometimes if user is already included in the group we need to Auto complete the request by adding work notes as "request already exists " in the group , need your help in understanding how we can tackle this Scenario
I have following script for adding and removing members from the group for run script
var group = current.variables.group_name;
var usersToAdd = current.variables.addmemberlist; // give correct variable name here
var usersToRemove = current.variables.removememberlist;
var grp1 = new GlideRecord('sys_user_grmember');
grp1.addQuery('group',group);
grp1.addQuery('user',usersToRemove);
grp1.query();
while(grp1.next())
{
grp1.deleteMultiple();
}
var arr = usersToAdd.toString().split(',');
for (var i = 0; i < arr.length; i++) {
var addRec = new GlideRecord('sys_user_grmember');
addRec.addQuery("user", arr[i]);
addRec.addQuery("group", group);
addRec.query();
if (!addRec.hasNext()) {
gs.info("getting into if loop");
addRec.initialize();
addRec.user = arr[i];
addRec.group = group;
addRec.insert();
}
}
can you please give some suggestion how we can develop this kind of automation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 05:19 AM
I would suggest you start from your side. it's easy to get a user's group by querying sys_user_grmember table
Also to show only those groups where user is not member just reverse the query
Something like this
Are you doing this for logged in user then pass gs.getUserID() in the advanced ref qualifier
Add Groups Variable
javascript: new checkRecords().notMyGroups();
Remove Groups Variable
javascript: new checkRecords().getMyGroups();
var checkRecords = Class.create();
checkRecords.prototype = {
initialize: function() {
},
getMyGroups: function(user){
var arr = [];
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", user);
gr.query();
while(gr.next()) {
arr.push(gr.getValue('group'));
}
return 'sys_idIN' + arr.toString();
},
notMyGroups: function(user){
var arr = [];
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", user);
gr.query();
while(gr.next()) {
arr.push(gr.getValue('group'));
}
return 'sys_idNOT IN' + arr.toString();
},
type: 'checkRecords'
};
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 05:37 AM
Hello @Ankur Bawiskar ,
I have an issue with this logic - while removing the users , my variable type is "list collector"
var grp1 = new GlideRecord('sys_user_grmember');
grp1.addQuery('group',group);
grp1.addQuery('user',usersToRemove);
grp1.query();
while(grp1.next())
{
grp1.deleteMultiple();
}
it is deleting the record but under the group member it is showing as below (empty it is not completely getting removed)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 05:41 AM
try this since it's list collector
var grp1 = new GlideRecord('sys_user_grmember');
grp1.addQuery('group','IN',group);
grp1.addQuery('user',usersToRemove);
grp1.query();
while(grp1.next())
{
grp1.deleteMultiple();
}
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 06:25 AM
@Ankur Bawiskar Still its same Ankur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 07:03 AM
Sorry but I am not completely aware on what variables are there and what are the types?
Also not sure where are you using the above script.
I believe I already provided guidance on how to avoid the situation
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader