Skip to content

Commit f1cbe83

Browse files
authored
Dynamic business rule to update user roles based on department changes (#1108)
* Add files via upload * Create code.js This script contains two parts, 1st part is client script and 2nd part is script include.. and this helps to lock the file till the developer completes his development, so that we can avoid concurrency issues. Ex: If a "Developer A" working on "Widget A", parallelly if "Developer B" working on "Widget B", then if Developer B moves the code to higher environment before developer A completes his development.. so it creates an issue.. to avoid these kind of concurrency issues.. we can use locking system for each record.. in the above script I shown example for widget records. * Delete code.js * Delete DisplayTableandFieldNames.js * Delete readme.md.txt * Create code.js Calculate the time difference between incident creation and change implementation * code.js Set due date to incidents based on their priority and the assignment group's working hours * Delete Business Rules/Set due date to incidents based on their priority and the assignment group's working hours directory * Delete Background Scripts/Calculate time difference between incident creation and related change implementation directory * Create Dynamic Business Rule to Update User Roles Based on Department Changes.js * Create readme.md
1 parent 52f4eec commit f1cbe83

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
var newDepartment = current.department.getDisplayValue();
4+
5+
// Clear existing roles
6+
var roleGR = new GlideRecord('sys_user_has_role');
7+
roleGR.addQuery('user', current.sys_id);
8+
roleGR.deleteMultiple(); // Remove existing roles
9+
10+
// Assign new roles based on the new department
11+
var newRoleGR = new GlideRecord('sys_role');
12+
newRoleGR.addQuery('name', 'LIKE', newDepartment); // Assuming role names follow department names
13+
newRoleGR.query();
14+
15+
while (newRoleGR.next()) {
16+
var userRoleGR = new GlideRecord('sys_user_has_role');
17+
userRoleGR.initialize();
18+
userRoleGR.user = current.sys_id;
19+
userRoleGR.role = newRoleGR.sys_id;
20+
userRoleGR.insert();
21+
}
22+
23+
gs.info('Updated roles for user ' + current.user_name + ' based on new department: ' + newDepartment);
24+
}
25+
})(current, previous);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**Purpose**
2+
3+
This business rule automatically updates user roles in ServiceNow whenever a user's department changes. By ensuring that users have the appropriate roles based on their current department, the rule helps maintain data integrity and enhances access control within the organization.
4+
5+
6+
7+
**Complete Business Rule Configuration**
8+
9+
**Name**: "Update User Roles Based on Department Changes"
10+
11+
**Table**: sys_user
12+
13+
**When**: Before
14+
15+
**Insert**: Checked
16+
17+
**Update**: Checked
18+
19+
20+
**Condition**:
21+
22+
**javascript**
23+
24+
'''current.department.changes();'''

0 commit comments

Comments
 (0)