- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2024 04:39 AM
Hi All,
I have created below script to 1) find all the ‘Group’' managers 2) validate if they are part of another group ‘ITSM-Managers’
and 3) retrieve the list of ‘Group’ managers who are not present in the 'ITSM-Managers’ group
I have a requirement to create a schedule job to add/remove users to execute/trigger based on below condition.
-Add users who are not present in the 'ITSM-Managers’ group.
-Remove users from ITSM-Managers’ group if they are not Group managers
var out = [];
var grManagers = new GlideRecord("sys_user_group");
grManagers.addEncodedQuery("manager!=NULL");
grManagers.query();
while (grManagers.next()) {
//loop through IT Managers group members to check if they are part of or not
var member = new GlideRecord("sys_user_grmember");
member.addQuery("group", "c38f00f4530360100999ddeeff7b1298"); //sys_id of the ITSM-Managers group
member.query();
while (member.next()) {
if (member.getValue("user")! == grManagers.getValue("manager")) {
out.push(member.getDisplayValue("user"));
}
}
}
gs.info("Managers who are not in the ITSM-Managers group::" + " " + out);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 01:01 AM
Hello @JP6
If this solution worked for you, can you please mark it as an accepted solution.
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-24-2024 11:35 PM
Here @JP6
Here is the updated script:
itsmMembers.forEach(function (user, index) {
if (groupManagers.indexOf(user) === -1) { // Check if user is not in groupManagers
var grRemove = new GlideRecord("sys_user_grmember");
grRemove.addQuery("group", ITSM_GROUP_ID);
grRemove.addQuery("user", user);
grRemove.query();
if (grRemove.next()) {
grRemove.deleteRecord();
gs.info("Removed user " + user + " from ITSM-Managers group.");
} else {
gs.warn("Failed to find record for user " + user + " in ITSM-Managers group for removal.");
}
}
});
Note: replaced includes() function with indexOf() function.
This should work. Please try and let me know.
"If you found my answer helpful, please give like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 01:01 AM
Hello @JP6
If this solution worked for you, can you please mark it as an accepted solution.
Thank You
Juhi Poddar