automatically adding users to group via business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 10:38 AM
Hello,
I am trying to create a business rule that after a user record has been inserted or updated, that it looks for their job title and depending on that value, it then adds them to a particular group. Right now i just noticed that each time I updated the user record it adds a duplicate group membership record for the user. How can I prevent the duplication?
My script as of now is below
var rec = new GlideRecord('sys_user_grmember');
rec.initialize();
rec.user = current.sys_id;
rec.group = 'group sys ID here';
rec.insert();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2024 11:41 AM
@mkerkemeyer Please update your BR script as follows.
var list = new GlideAggregate("sys_user_grmember");
list.addQuery('group','<group sys_id>');//Add sys_id of your group here
list.addQuery('user',current.sys_id);
list.addAggregate('COUNT');
list.query();
var count = 0;
if (list.next()){
count= list.getAggregate('COUNT');
}
if(count==0){
var rec = new GlideRecord('sys_user_grmember');
rec.initialize();
rec.user = current.sys_id;
rec.group = 'group sys ID here';
rec.insert();
}
Hope this helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2024 04:16 PM
This works in my pdi, but for some reason in our dev it says this after submitting the risk event "you do not have permission to modify this group membership. The users in testing have 0 roles, and the use case is if they submit a risk event, then it should give them the role which we do by group. I am going to ask our team on Monday if we have something blocking the business rule, but your code is clean.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2024 08:59 PM
Hello @mkerkemeyer ,
Please Update your code with any one of below code in business rule
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery('group',''group sys ID here'');
rec.addQuery('user',current.sys_id);
rec.query();
if(!rec.hasNext())
{
rec.initialize();
rec.user = current.sys_id;
rec.group = 'group sys ID here';
rec.insert();
}
(or)
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery('group',''group sys ID here'');
rec.addQuery('user',current.sys_id);
rec.query();
if(rec.getRowCount()<1)
{
rec.initialize();
rec.user = current.sys_id;
rec.group = 'group sys ID here';
rec.insert();
}