Skip to content

Commit 8afecf3

Browse files
authored
Create IdentifyInactiveGrp.js
This schedule script will execute monthly, weekly or bi-weekly as frequency is scheduled to identify all the groups that are not referenced in any Task(INC/PRD/CHG/RITM) in last 6 month and mark them INACTIVE. This activity has been carried out as cleanup activity for groups that are not in use and consuming license role
1 parent 01bb3d8 commit 8afecf3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var sixMonthsAgo = new GlideDateTime();
2+
sixMonthsAgo.addMonthsUTC(-6);
3+
4+
var inactiveGroups = [];
5+
6+
var grGroups = new GlideRecord('sys_user_group');
7+
grGroups.addInactiveQuery(); // To find all groups that are Active = False in sys_user_group table
8+
grGroups.addActiveQuery(); // To find all groups that are Active = True in sys_user_group table
9+
grGroups.query();
10+
11+
while (grGroups.next()) {
12+
var groupSysId = grGroups.getValue('sys_id');
13+
var groupName = grGroups.getValue('name');
14+
15+
// Check if the group has any task assigned in the last 6 months
16+
var taskCheck = new GlideAggregate('task');
17+
taskCheck.addQuery('assignment_group', groupSysId); //filter to only include tasks where the assignment group matches the current group's sys_id.
18+
taskCheck.addQuery('sys_created_on', '>', sixMonthsAgo); //filter to only include tasks that were created after the date 6 months ago.
19+
taskCheck.addAggregate('COUNT');
20+
taskCheck.query();
21+
22+
//If the count is greater than 0, the group has task activity, so it's not inactive.
23+
24+
var isInactive = true;
25+
if (taskCheck.next()) {
26+
var count = parseInt(taskCheck.getAggregate('COUNT'), 10);
27+
if (count > 0) {
28+
isInactive = false;
29+
}
30+
}
31+
32+
// Add only unique group names
33+
if (isInactive && inactiveGroups.indexOf(groupName) === -1) {
34+
inactiveGroups.push(groupName);
35+
}
36+
}
37+
38+
// Output all inactive group names as a comma-separated string
39+
40+
if (inactiveGroups.length > 0) {
41+
    var groupArrayString = '["' + inactiveGroups.join('", "') + '"]';
42+
    gs.log('Inactive groups (last 6 months) as array: ' + groupArrayString);
43+
} else {
44+
    gs.log('No inactive groups found in the last 6 months.');
45+
}

0 commit comments

Comments
 (0)