Skip to content

Commit 3640036

Browse files
authored
Fix script pull (#2164)
* Create README.md This README explains how to configure and use the fix script. * Create fix_script.js This Fix Script ensures all users in a specified group have the same roles assigned to that group. If any user is missing group roles, they are temporarily removed and re-added to the group to refresh their role assignments.
1 parent 2db414f commit 3640036

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
ServiceNow Fix Script - Group Role Synchronization
2+
Overview
3+
4+
This Fix Script automatically validates and synchronizes user roles with their assigned groups in ServiceNow.
5+
It checks if every user in the target groups has all the roles assigned to that group.
6+
If any roles are missing, the script re-adds the user to the group, ensuring all inherited roles are correctly applied.
7+
8+
How It Works
9+
10+
Identify Groups
11+
The script starts by reading the list of sys_ids of the target groups.
12+
13+
Fetch Group Roles
14+
It retrieves all the roles assigned to each group from the sys_group_has_role table.
15+
16+
Check Each User
17+
For each user in the group (sys_user_grmember), it fetches their assigned roles from sys_user_has_role.
18+
19+
Detect Missing Roles
20+
Compares the user’s roles with the group’s roles.
21+
If any group role is missing for a user:
22+
23+
Removes the user from the group.
24+
25+
Re-adds the user to the group, triggering ServiceNow’s role inheritance process.
26+
27+
Logs
28+
The script logs all actions using gs.info() for easy monitoring in the system logs.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
(function executeFixScript() {
2+
// List of group sys_ids to process
3+
var groupIds = [
4+
'a715cd759f2002002920bde8132e7018' // Add more sys_ids if needed
5+
];
6+
7+
var groupGR = new GlideRecord('sys_user_group');
8+
groupGR.addQuery('sys_id', 'IN', groupIds);
9+
groupGR.query();
10+
11+
while (groupGR.next()) {
12+
gs.info('Processing Group: ' + groupGR.name);
13+
14+
// --- Fetch all roles assigned to this group ---
15+
var groupRoles = [];
16+
var groupRoleGR = new GlideRecord('sys_group_has_role');
17+
groupRoleGR.addQuery('group', groupGR.sys_id);
18+
groupRoleGR.query();
19+
20+
while (groupRoleGR.next()) {
21+
groupRoles.push(groupRoleGR.role.toString());
22+
}
23+
24+
gs.info(' Group Roles: ' + groupRoles.join(', '));
25+
26+
// --- Get all users in the group ---
27+
var usersInGroup = [];
28+
var memberGR = new GlideRecord('sys_user_grmember');
29+
memberGR.addQuery('group', groupGR.sys_id);
30+
memberGR.query();
31+
32+
while (memberGR.next()) {
33+
var userGR = memberGR.user.getRefRecord();
34+
if (userGR.isValidRecord()) {
35+
usersInGroup.push({
36+
userRecord: userGR,
37+
memberSysId: memberGR.sys_id
38+
});
39+
}
40+
}
41+
42+
// --- Validate each user's roles against group roles ---
43+
for (var i = 0; i < usersInGroup.length; i++) {
44+
var member = usersInGroup[i];
45+
var userGR = member.userRecord;
46+
47+
// Collect all roles assigned to user
48+
var userRoles = [];
49+
var userRoleGR = new GlideRecord('sys_user_has_role');
50+
userRoleGR.addQuery('user', userGR.sys_id);
51+
userRoleGR.query();
52+
53+
while (userRoleGR.next()) {
54+
userRoles.push(userRoleGR.role.toString());
55+
}
56+
57+
// Identify missing roles
58+
var missingRoles = groupRoles.filter(function(role) {
59+
return userRoles.indexOf(role) === -1;
60+
});
61+
62+
if (missingRoles.length > 0) {
63+
gs.info(' User ' + userGR.name + ' missing roles: ' + missingRoles.join(', '));
64+
gs.info(' Re-adding user to group to refresh roles.');
65+
66+
// Remove user from the group
67+
var deleteGR = new GlideRecord('sys_user_grmember');
68+
if (deleteGR.get(member.memberSysId)) {
69+
deleteGR.deleteRecord();
70+
}
71+
72+
// Re-add user to group to trigger role re-evaluation
73+
var newMember = new GlideRecord('sys_user_grmember');
74+
newMember.initialize();
75+
newMember.group = groupGR.sys_id;
76+
newMember.user = userGR.sys_id;
77+
newMember.insert();
78+
79+
gs.info(' User ' + userGR.name + ' re-added successfully.');
80+
} else {
81+
gs.info(' User ' + userGR.name + ' has all required roles.');
82+
}
83+
}
84+
85+
gs.info('Completed processing group: ' + groupGR.name);
86+
}
87+
88+
gs.info('Fix Script completed successfully for all specified groups.');
89+
})();

0 commit comments

Comments
 (0)