- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2024 01:39 PM
Can someone please help me with a BR to add users to a group?
If a user is active and is not a party of " All users" group, then add him/her to the group.
I created a BR rule using sys_user table with the following condition, but I need help with the code. Also, I prefer to run this business rule when there is an update in the user record only, so I'm assuming I will check only "Update"
Active = True
Created by = Azure
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2024 02:14 PM - edited 01-03-2024 02:15 PM
@Thomas99 : Here is the code that worked in my PDI. It's an after-update business rule with the conditions you mentioned.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('sys_user_grmember'); // Table stores the mapping of user and group.
gr.addQuery('user', current.sys_id.toString()); // sys_id of user.
gr.addQuery('group', '<sys_id_group>'); // replace <sys_id_group> with actual sys_id of group.
gr.query();
if (!gr.next()) {
gs.info("User is Not a Member of Group!");
// Logic to add to group
gr.initialize();
gr.user = current.sys_id.toString();
gr.group = '<sys_id_group>';
gr.insert();
}
})(current, previous);
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2024 02:14 PM - edited 01-03-2024 02:15 PM
@Thomas99 : Here is the code that worked in my PDI. It's an after-update business rule with the conditions you mentioned.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord('sys_user_grmember'); // Table stores the mapping of user and group.
gr.addQuery('user', current.sys_id.toString()); // sys_id of user.
gr.addQuery('group', '<sys_id_group>'); // replace <sys_id_group> with actual sys_id of group.
gr.query();
if (!gr.next()) {
gs.info("User is Not a Member of Group!");
// Logic to add to group
gr.initialize();
gr.user = current.sys_id.toString();
gr.group = '<sys_id_group>';
gr.insert();
}
})(current, previous);
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 06:20 AM
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2024 09:11 PM
Hi there,
"but I need help with the code". Did you consider using Flow Designer for this? Newer way of working, and for your use case most likely zero code involved.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 06:21 AM
Hi Mark...
Thank you. I did a test with flow designer and, yes it should work too 🙂 Thanks for the code-free suggestion.
Snedden