Skip to content

Commit 6863578

Browse files
Remove inactive users from Groups (#2275)
* README.md * pdfletter.js * Delete Server-Side Components/Business Rules/PdfletterGeneration directory * README.md * removeuser.js * Delete Specialized Areas/Fix scripts/Delete User Groups directory * README.md * removeuser.js
1 parent dddd3c4 commit 6863578

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
contribution
2+
A fix script is used to remove inactive users from the specified list of Groups. The script checks for users who have been inactive in the system more than 7 days from the current date and removes them from the respective groups accordingly. If users are inactive within 7 days then users will skip for deletion.
3+
4+
Additionally, if any of these users are assigned to existing open incidents, the "Assigned to" field on those open incidents will be cleared.
5+
6+
Groups:
7+
IT ServiceNow L1 IT ServiceNow L2 IT ServiceNow L3
8+
9+
Fix script Goal:
10+
Fix script 1name - Remove Inactive Users from Groups Record for Rollback - Enable check box
11+
12+
The goal of Fix script is to clean up inactive users from the above mentioned groups in ServiceNow, and unassign anyincidents they were responsible for — but only if they have been inactive for more than 7 days.
13+
14+
Taking Groups sysid in system Property with "," seperator
15+
16+
Queries group members (sys_user_grmember) where the group matches one of the specified groups.
17+
18+
Skips for Active users , Inactive users who were updated in the last 7 days.
19+
20+
Finds Incident assigned to the user that are not closed (assuming state = 3 means closed). Clears the assigned_to field and updates the record.
21+
22+
System Property : Store groups sysid.
23+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var SEVEN_DAYS_AGO = new GlideDateTime();
2+
3+
SEVEN_DAYS_AGO.addDaysUTC(-7);
4+
5+
//Get group sys_ids from system property
6+
7+
var groupSysIdsStr = gs.getProperty('inactiveuser.groupname');
8+
9+
if (!groupSysIdsStr) {
10+
11+
12+
13+
14+
}
15+
16+
var GroupSysIds = groupSysIdsStr.split(','); // Split grp sysid with ,
17+
18+
//group members in those groups
19+
20+
var groupMemberGR = new GlideRecord('sys_user_grmember');
21+
22+
groupMemberGR.addQuery('group', 'IN', GroupSysIds.join(','));
23+
24+
groupMemberGR.query();
25+
26+
while (groupMemberGR.next()) {
27+
28+
var userID = groupMemberGR.getValue('user');
29+
30+
if (!userID) continue;
31+
32+
var userGR = new GlideRecord('sys_user');
33+
34+
if (!userGR.get(userID)) continue;
35+
36+
if (userGR.active) continue; // Active user skip
37+
38+
var updatedOn = new GlideDateTime(userGR.sys_updated_on);
39+
40+
if (updatedOn.compareTo(SEVEN_DAYS_AGO) >= 0) { // User was updated within 7 days, so skip
41+
42+
// gs.info("Skipping user: " + userGR.name + " - updated on " + userGR.getDisplayValue('sys_updated_on'));
43+
44+
continue;
45+
46+
}
47+
48+
var groupName = groupMemberGR.group.getDisplayValue();
49+
50+
51+
52+
//Unassign Incident record
53+
54+
var inc = new GlideRecord('incident');
55+
56+
inc.addQuery('assigned_to', userID);
57+
58+
59+
inc.addQuery('state', '!=', '3');
60+
61+
inc.query();
62+
63+
while (inc.next()) {
64+
65+
66+
inc.assigned_to = '';
67+
68+
inc.update();
69+
70+
71+
72+
}
73+
74+
75+
76+
// Remove user from group
77+
78+
gs.info("Removing user: " + userGR.name + " from group: " + groupName);
79+
80+
groupMemberGR.deleteRecord();
81+
82+
}

0 commit comments

Comments
 (0)