Skip to content

Add code snippet to manage group membership automatically via API #1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Overview
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.

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.

# How It Works
The script:
- Fetches API Data: It connects to an external API (specified by the `apiEndpoint` variable) to retrieve the current group membership details.
- Parses API Response: The response is parsed to extract user information (based on email) and group identifiers.
- Updates Group Memberships:
- For each member in the response, the script queries the `sys_user` table to locate the user in ServiceNow based on their email address.
- Once a user is found, the script creates a new record in the `sys_user_grmember` table, associating the user with the appropriate group.

# Implementation
- Define the `apiEndpoint` URL, replacing `https://your-group-api.com/members` with the actual endpoint from which group membership data will be fetched.
- Ensure that any necessary authentication for the API is configured, such as API keys or tokens.
- This script uses email as a unique identifier for users. Adjust `userGR.addQuery('email', member.email)`; if another identifier is needed.
- 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Script to update group memberships based on API data
(function executeRule(current, previous /*null when async*/) {
var apiEndpoint = 'https://your-group-api.com/members';
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(apiEndpoint);
request.setHttpMethod('GET');

var response = request.execute();
var responseData = JSON.parse(response.getBody());

// Update group memberships
responseData.members.forEach(function(member) {
var userGR = new GlideRecord('sys_user');
userGR.addQuery('email', member.email);
userGR.query();
Comment on lines +13 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use setLimit(1) when only looking for a single record


if (userGR.next()) {
var groupMembership = new GlideRecord('sys_user_grmember');
groupMembership.initialize();
groupMembership.group = member.group_id;
groupMembership.user = userGR.sys_id;
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In future, use getters and setters (getValue('field') and setValue('field',value)). For sys_ids that are the unique value for a record, use getUniqueValue()

groupMembership.insert();
}
});
})(current, previous);
Loading