Skip to content

Commit cf4887a

Browse files
authored
Auto role assignment by department (#1555)
1 parent c577b1b commit cf4887a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Overview
2+
This snippet for ServiceNow developers automate the process of assigning roles to users based on their department. It helps to simplify user role management, especially useful in organizations where specific departments require predefined access levels.
3+
4+
# How It Works
5+
In Business Rule settings within ServiceNow:
6+
- Trigger: runs the script "before" an update to the `sys_user` table when a user’s department changes.
7+
- Condition: Only triggers when `current.department.changes()` - ensures that the script only runs when the department field is modified.
8+
- Role Mapping: Uses the `rolesByDepartment` dictionary to assign roles based on the user’s department.
9+
10+
# Implementation
11+
12+
Edit `rolesByDepartment` to match your organization’s needs.
13+
14+
```
15+
var rolesByDepartment = {
16+
<department_name> : [role_1, role_2]
17+
};
18+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Script to automatically assign roles based on user department
2+
(function executeRule(current, previous /*null when async*/) {
3+
var department = current.department.getDisplayValue();
4+
5+
// Define roles by department
6+
var rolesByDepartment = {
7+
"IT": ["itil", "asset"],
8+
"HR": ["hr_manager", "employee"],
9+
"Finance": ["finance_analyst", "approver"]
10+
};
11+
12+
// Remove existing roles
13+
current.roles = [];
14+
15+
// Assign new roles based on department
16+
if (rolesByDepartment[department]) {
17+
rolesByDepartment[department].forEach(function(role) {
18+
current.roles.add(role);
19+
});
20+
current.update();
21+
}
22+
})(current, previous);

0 commit comments

Comments
 (0)