Skip to content

Commit ccbd725

Browse files
authored
Adding code snippet to manage group memembership automatically via API (#1580)
1 parent 2bc8f85 commit ccbd725

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-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 code snippet helps ServiceNow developers manage group memberships automatically by integrating with an external API. It retrieves group membership data from a specified API endpoint and updates user-group relationships in ServiceNow accordingly.
3+
4+
This is useful for organizations where user groups are managed dynamically in external systems, and developer want a seamless and up-to-date integration with ServiceNow.
5+
6+
# How It Works
7+
The script:
8+
- Fetches API Data: It connects to an external API (specified by the `apiEndpoint` variable) to retrieve the current group membership details.
9+
- Parses API Response: The response is parsed to extract user information (based on email) and group identifiers.
10+
- Updates Group Memberships:
11+
- For each member in the response, the script queries the `sys_user` table to locate the user in ServiceNow based on their email address.
12+
- Once a user is found, the script creates a new record in the `sys_user_grmember` table, associating the user with the appropriate group.
13+
14+
# Implementation
15+
- Define the `apiEndpoint` URL, replacing `https://your-group-api.com/members` with the actual endpoint from which group membership data will be fetched.
16+
- Ensure that any necessary authentication for the API is configured, such as API keys or tokens.
17+
- This script uses email as a unique identifier for users. Adjust `userGR.addQuery('email', member.email)`; if another identifier is needed.
18+
- Deploy the script as a Business Rule in ServiceNow, setting the appropriate table and conditions under which it should execute. For example, it could run on a schedule or be triggered by a specific update.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Script to update group memberships based on API data
2+
(function executeRule(current, previous /*null when async*/) {
3+
var apiEndpoint = 'https://your-group-api.com/members';
4+
var request = new sn_ws.RESTMessageV2();
5+
request.setEndpoint(apiEndpoint);
6+
request.setHttpMethod('GET');
7+
8+
var response = request.execute();
9+
var responseData = JSON.parse(response.getBody());
10+
11+
// Update group memberships
12+
responseData.members.forEach(function(member) {
13+
var userGR = new GlideRecord('sys_user');
14+
userGR.addQuery('email', member.email);
15+
userGR.query();
16+
17+
if (userGR.next()) {
18+
var groupMembership = new GlideRecord('sys_user_grmember');
19+
groupMembership.initialize();
20+
groupMembership.group = member.group_id;
21+
groupMembership.user = userGR.sys_id;
22+
groupMembership.insert();
23+
}
24+
});
25+
})(current, previous);

0 commit comments

Comments
 (0)