diff --git a/Specialized Areas/Fix scripts/Remove User Groups/README.md b/Specialized Areas/Fix scripts/Remove User Groups/README.md new file mode 100644 index 0000000000..a5b9885ee0 --- /dev/null +++ b/Specialized Areas/Fix scripts/Remove User Groups/README.md @@ -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. + diff --git a/Specialized Areas/Fix scripts/Remove User Groups/removeuser.js b/Specialized Areas/Fix scripts/Remove User Groups/removeuser.js new file mode 100644 index 0000000000..eb25b9e2b9 --- /dev/null +++ b/Specialized Areas/Fix scripts/Remove User Groups/removeuser.js @@ -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(); + +}