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

Conversation

wendy-ha18
Copy link
Contributor

@wendy-ha18 wendy-ha18 commented Oct 30, 2024

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 organisations where user groups are managed dynamically in external systems, and developer want a seamless and up-to-date integration with ServiceNow.

Folder structure:

  • Business Rules/Automatic Group Membership Updates via API/autoGroupMembershipUpdate.js
  • Business Rules/Automatic Group Membership Updates via API/README.md

P/S: @Lacah or any maintainers that will review this PR, please help to point me to the appropriate top-level folder that suitable with it, but I think it's match with Business Rules folder.

The Code Snippets repo is a bit big so sometime I have a confusion where I should put my code.

@wendy-ha18 wendy-ha18 changed the title Adding code snippet to manage group membership automatically via API Add code snippet to manage group membership automatically via API Oct 30, 2024
@SapphicFire SapphicFire self-assigned this Oct 31, 2024
Copy link
Contributor

@SapphicFire SapphicFire left a comment

Choose a reason for hiding this comment

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

Looks good to me, I have included some advice for future

Comment on lines +20 to +21
groupMembership.group = member.group_id;
groupMembership.user = userGR.sys_id;
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()

Comment on lines +13 to +15
var userGR = new GlideRecord('sys_user');
userGR.addQuery('email', member.email);
userGR.query();
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

@SapphicFire SapphicFire merged commit ccbd725 into ServiceNowDevProgram:main Oct 31, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants