Skip to content

Commit fa48968

Browse files
authored
Added Readme and script files
1 parent 93891d6 commit fa48968

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Group Membership Utility
2+
3+
The **Group Membership Utility** is a ServiceNow server-side tool designed to streamline the management of user membership in assignment groups. It provides administrators with two UI actions on the Assignment Group table to add or remove themselves from a group, ensuring efficient group membership management. Super helpful when doing group membership testing.
4+
5+
## Challenge
6+
7+
Managing assignment group memberships manually can be time-consuming and frustrating when doing group change related testings.
8+
9+
## Features
10+
11+
- **Add Me**: Adds the current user to the selected assignment group, ensuring quick inclusion.
12+
- **Remove Me**: Removes the current user from the selected assignment group, simplifying group updates.
13+
- **Admin-Only Visibility**: Both actions are restricted to users with administrative privileges i.e admin user role, ensuring controlled access.
14+
15+
## Functionality
16+
17+
The Group Membership Utility provides the following capabilities:
18+
- Detects the current user's membership status in the selected group.
19+
- Dynamically enables or disables the **Add Me** and **Remove Me** actions based on the user's membership.
20+
- Ensures visibility of these actions only for users with administrative privileges.
21+
22+
## Visibility
23+
24+
Add below condition script for the "Add Me" UI action
25+
```javascript
26+
gs.hasRole('admin') && !gs.getUser().isMemberOf(current.sys_id);
27+
```
28+
Add below condition script for the "Remove Me" UI action
29+
```javascript
30+
gs.hasRole('admin') && gs.getUser().isMemberOf(current.sys_id);
31+
```
32+
33+
## Usage Instructions
34+
35+
1. Navigate to the Assignment Group table.
36+
2. Select a group.
37+
3. Use the **Add Me** button to add yourself to the group if you're not already a member.
38+
4. Use the **Remove Me** button to remove yourself from the group if you're already a member.
39+
40+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
try {
2+
var groupSysId = current.sys_id;
3+
var userSysId = gs.getUserID();
4+
5+
// Validate input
6+
if (!groupSysId || !userSysId) {
7+
throw new Error("Group Sys ID and User Sys ID are required.");
8+
}
9+
10+
// Create a new record in the sys_user_grmember table
11+
var gr = new GlideRecord("sys_user_grmember");
12+
gr.initialize();
13+
gr.group = groupSysId;
14+
gr.user = userSysId;
15+
var sysId = gr.insert();
16+
17+
if (sysId) {
18+
gs.addInfoMessage(
19+
"User successfully added to the group. Record Sys ID: " + sysId
20+
);
21+
} else {
22+
throw new Error("Failed to add user to the group.");
23+
}
24+
} catch (error) {
25+
gs.addErrorMessage("Error adding user to group: " + error.message);
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
try {
2+
var groupSysId = current.sys_id;
3+
var userSysId = gs.getUserID();
4+
5+
// Validate input
6+
if (!groupSysId || !userSysId) {
7+
throw new Error("Group Sys ID and User Sys ID are required.");
8+
}
9+
10+
// Query the sys_user_grmember table to find the record
11+
var gr = new GlideRecord("sys_user_grmember");
12+
gr.addQuery("group", groupSysId);
13+
gr.addQuery("user", userSysId);
14+
gr.query();
15+
16+
if (gr.next()) {
17+
// Delete the record
18+
var deleted = gr.deleteRecord();
19+
if (deleted) {
20+
gs.addInfoMessage("User successfully removed from the group.");
21+
} else {
22+
throw new Error("Failed to remove user from the group.");
23+
}
24+
} else {
25+
throw new Error("No matching record found for the user in the group.");
26+
}
27+
} catch (error) {
28+
gs.addErrorMessage("Error removing user from group: " + error.message);
29+
}

0 commit comments

Comments
 (0)