Add existing users to group through script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 02:18 PM
I want to add existing users to group and create a business rule to check if the user exist in the group if not add to the group
Please can someone help with script
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 02:41 PM
Hello,
Below is a script that you can use one time to create the relationship.
var user = new GlideRecord("sys_user");
user.addActiveQuery();
user.query;
while (user.next()){
var rel = new GlideRecord("sys_user_grmember");
rel.initialize();
rel.user = user.sys_id+"";
rel.group=[sys_id of the group];
rel.insert();
}
Based on it, please feel free to adjust it to your needs.
For example:
when creating a new user you want him to a group, with an initial check;
the below should be the script for the BR which runs on Insert - ASync on the sys_user table.
var userGroup = new GlideRecord("sys_user_grmember");
userGroup.addEncodedQuery("group="+sys_if of group+"^user="+current.getUniqueValue());
userGroup.query();
if (!userGroup.next()){
var rel = new GlideRecord("sys_user_grmember");
rel.initialize();
rel.user = current.getUniqueValue();
rel.group=[sys_id of the group];
rel.insert();
}
Hope this helps!
Tudor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 05:42 PM
Is it possible to do it using glide aggregate
?
Do you have script for it?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 04:19 PM
Hi J,
Following business rule on User table will add group to the user if the user is not yet a member.
(function executeRule(current, previous /*null when async*/ ) {
var groupSysId = '<sys_id of group to check>'; // edit to add sys_id of group to add if not yet a member
var grUserGroup = new GlideRecord('sys_user_grmember');
grUserGroup.addQuery('group', groupSysId);
grUserGroup.addQuery('user', current.sys_id);
grUserGroup.query();
if (!grUserGroup.hasNext()) {
grUserGroup.initialize();
grUserGroup.group = groupSysId;
grUserGroup.user = current.sys_id;
grUserGroup.insert();
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2022 05:21 PM
BTW, the above script can be rewritten to use group name instead of group id.
(function executeRule(current, previous /*null when async*/ ) {
var groupName = '<name of group>';
var grUserGroup = new GlideRecord('sys_user_grmember');
grUserGroup.addQuery('group.name', groupName);
grUserGroup.addQuery('user', current.sys_id);
grUserGroup.query();
if (!grUserGroup.hasNext()) {
grUserGroup.initialize();
grUserGroup.setDisplayValue('group', groupName);
grUserGroup.user = current.sys_id;
grUserGroup.insert();
}
})(current, previous);