Trying to delete record from group via business rule.

Community Alums
Not applicable

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.

4 REPLIES 4

Mike_R
Kilo Patron
Kilo Patron

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();

Community Alums
Not applicable

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.

 

cynlink1
Tera Expert

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!

Tony Chatfield1
Kilo Patron

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.