Trying to delete record from group via business rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2022 10:01 AM - edited 11-02-2022 10:03 AM
Hii,
Actually I am trying to delete user from group on a condition if he is not managed by to any configuration item before.
I am writing my code here please have look and suggest me why I am not getting the desired result.
(function executeRule(current, previous /*null when async*/) {
if(current.managed_by != previous.managed_by)
{
var g=new GlideRecord('sys_user_grmember');
g.initialize();
g.group='d2f9386a9776511061f33e66f053afc5';
g.user=current.managed_by;
g.insert();
var ga=new GlideRecord('cmdb_ci');
ga.addQuery('managed_by',previous.managed_by);
ga.query();
if(!ga.next())
{
var gr=new GlideRecord('sys_user_grmember');
gr.addQuery('group','d2f9386a9776511061f33e66f053afc5');
gr.addQuery('user',previous.managed_by);
gr.query();
if(gr.next())
{
gr.deleteRecord();
}
}
}
})(current, previous);
Condition is When Managed By Changes
After Business rule.
Thank You In advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2022 10:08 AM
Is there a reason why you are inserting a new record here?
var g = new GlideRecord('sys_user_grmember');
g.initialize();
g.group = 'd2f9386a9776511061f33e66f053afc5';
g.user = current.managed_by;
g.insert();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2022 10:10 AM
Yes.. When user update the managed by field in CI then the previous user will be deleted on the condition and new user will be added to group.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2023 11:44 PM
Hello Somu,
I am encountering a similar issue. Were you able to get your script to work? If yes, would you be willing to share the working code?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2023 01:37 AM
Hi, testing your code in a PDI I find it works correctly and the group membership record for the previous managed_by user is deleted if the user is not the managed_by user of any other cmdb_ci records.