-
Notifications
You must be signed in to change notification settings - Fork 679
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
SapphicFire
merged 1 commit into
ServiceNowDevProgram:main
from
wendy-ha18:feature/auto-group-membership-update
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
Business Rules/Automatic Group Membership Updates via API/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
25 changes: 25 additions & 0 deletions
25
Business Rules/Automatic Group Membership Updates via API/autoGroupMembershipUpdate.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In future, use getters and setters ( |
||
groupMembership.insert(); | ||
} | ||
}); | ||
})(current, previous); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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