Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Specialized Areas/Fix scripts/Remove User Groups/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
contribution
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.

Additionally, if any of these users are assigned to existing open incidents, the "Assigned to" field on those open incidents will be cleared.

Groups:
IT ServiceNow L1 IT ServiceNow L2 IT ServiceNow L3

Fix script Goal:
Fix script 1name - Remove Inactive Users from Groups Record for Rollback - Enable check box

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.

Taking Groups sysid in system Property with "," seperator

Queries group members (sys_user_grmember) where the group matches one of the specified groups.

Skips for Active users , Inactive users who were updated in the last 7 days.

Finds Incident assigned to the user that are not closed (assuming state = 3 means closed). Clears the assigned_to field and updates the record.

System Property : Store groups sysid.

82 changes: 82 additions & 0 deletions Specialized Areas/Fix scripts/Remove User Groups/removeuser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var SEVEN_DAYS_AGO = new GlideDateTime();

SEVEN_DAYS_AGO.addDaysUTC(-7);

//Get group sys_ids from system property

var groupSysIdsStr = gs.getProperty('inactiveuser.groupname');

if (!groupSysIdsStr) {




}

var GroupSysIds = groupSysIdsStr.split(','); // Split grp sysid with ,

//group members in those groups

var groupMemberGR = new GlideRecord('sys_user_grmember');

groupMemberGR.addQuery('group', 'IN', GroupSysIds.join(','));

groupMemberGR.query();

while (groupMemberGR.next()) {

var userID = groupMemberGR.getValue('user');

if (!userID) continue;

var userGR = new GlideRecord('sys_user');

if (!userGR.get(userID)) continue;

if (userGR.active) continue; // Active user skip

var updatedOn = new GlideDateTime(userGR.sys_updated_on);

if (updatedOn.compareTo(SEVEN_DAYS_AGO) >= 0) { // User was updated within 7 days, so skip

// gs.info("Skipping user: " + userGR.name + " - updated on " + userGR.getDisplayValue('sys_updated_on'));

continue;

}

var groupName = groupMemberGR.group.getDisplayValue();



//Unassign Incident record

var inc = new GlideRecord('incident');

inc.addQuery('assigned_to', userID);


inc.addQuery('state', '!=', '3');

inc.query();

while (inc.next()) {


inc.assigned_to = '';

inc.update();



}



// Remove user from group

gs.info("Removing user: " + userGR.name + " from group: " + groupName);

groupMemberGR.deleteRecord();

}
Loading