Business rules Adding user to group Multiple times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 04:23 AM
Hi All,
I have business rule with runs After ( insert and Update ) this BR runs if any user update or created those users will add to the group based on user and group domain. Br working But the issue is,
if user update multiple time its add the user to the same group Multiple times. So we need to fix if user is already in group it should not add again.
var rec = new GlideRecord('u_uis_client_lookup');
rec.addQuery('u_type', 'password_reset');
rec.addQuery('sys_domain', current.sys_domain);
rec.query();
while (rec.next()) {
var groupp = rec.getValue('u_group_reference');
var rec1 = new GlideRecord('sys_user_grmember');
rec1.initialize();
rec1.group = groupp;
rec1.user = current.sys_id;
rec1.insert();
}
Please suggest.
Thanks all,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 04:30 AM
Hi @varma2,
You can have an additional query to verify the existence of the user in the specified group.
Something like :
var userSysId = current.getUniqueValue();
var grUISCL = new GlideRecord('u_uis_client_lookup');
grUISCL.addQuery('u_type', 'password_reset');
grUISCL.addQuery('sys_domain', current.sys_domain);
grUISCL.query();
while (grUISCL.next()) {
var groupp = grUISCL.getValue('u_group_reference');
var grGrMember = new GlideRecord('sys_user_grmember');
grGrMember.addQuery('user', userSysId);
grGrMember.addQuery('group', groupp);
grGrMember.query();
if (!grGrMember.hasNext()) {
grGrMember.initialize();
grGrMember.setValue('user', userSysId);
grGrMember.setValue('group', groupp);
grGrMember.insert();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 05:26 AM - edited 09-14-2023 05:28 AM
To prevent adding the same user to the group multiple times in your business rule, you should first check if the user is already a member of the group before attempting to insert a new record. Please use the below code.
var rec = new GlideRecord('u_uis_client_lookup');
rec.addQuery('u_type', 'password_reset');
rec.addQuery('sys_domain', current.sys_domain);
rec.query();
while (rec.next()) {
var groupp = rec.getValue('u_group_reference');
// Check if the user is already a member of the group
var rec1 = new GlideRecord('sys_user_grmember');
rec1.addQuery('group', groupp);
rec1.addQuery('user', current.sys_id);
rec1.query();
if (!rec1.hasNext()) {
// User is not a member of the group, so add them
var rec2 = new GlideRecord('sys_user_grmember');
rec2.initialize();
rec2.group = groupp;
rec2.user = current.sys_id;
rec2.insert();
}
}
Mark the comment as a correct answer and also helpful if this has helped to solve the problem.
Krishna Sharma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 06:26 AM
@varma2 if this solved your issue please accept the solution so that others can benefit from the content , thank you
Krishna Sharma