Business Rule to remove users from a group

keithlyon
Kilo Contributor

I have seen several threads on this subject, but none of the solutions proposed seem to work for me, so I thought I would put this out there. I have 2 business rules to add users to a group based on criteria changing in the Sys_User table. the first rule, which is to add a user to the group is working perfectly. It is set to run after and when the Supervisor Flag (user defined field) changes to true on insert or update.

var gr = new GlideRecord('sys_user_grmember');

gr.initiate();

gr.user = current.sys_id;

gr.group = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//sys_id of the group to be added to

gr.insert();

the 2nd rule is the remove from group when the supervisor flag changes to false on update.

var gt = new GlideRecord('sys_user_grmember');

gt.initiate();

gt.user = current.sys_id;

gt.group = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';//sys_id of the group to be removed from

gt.deleteRecord();

I am probably oversimplifying by simply reversing the add rule and using delete record instead of insert. Any thoughts on what I can try to make this work?

1 ACCEPTED SOLUTION

** Copying Jaspal's code here with some fixes



var gt = new GlideRecord('sys_user_grmember');


gt.addQuery('user',current.sys_id);


gt.addQuery('group','sys_id_if_group');


gt.query();


while(gt.next())


{


gt.deleteRecord();


}



Let me know if that works



Cheers,



Dylan


View solution in original post

5 REPLIES 5

Harsh Vardhan
Giga Patron

to delete can you try with script below.




var abc = new GlideRecord("sys_user_grmember");  


    abc.addQuery('user', current.sys_id);  


    abc.query();  


    while (abc.next())  


          abc.deleteRecord();  


}  


Jaspal Singh
Mega Patron
Mega Patron

Hi Keith,



Try using below snippet



var gt = new GlideRecord('sys_user_grmember');


gt.addQuery('user',current.sys_id);


gr.addQuery('group','sys_id_if_group');


while(gt.next())


{


gt.delete();


}


** Copying Jaspal's code here with some fixes



var gt = new GlideRecord('sys_user_grmember');


gt.addQuery('user',current.sys_id);


gt.addQuery('group','sys_id_if_group');


gt.query();


while(gt.next())


{


gt.deleteRecord();


}



Let me know if that works



Cheers,



Dylan


Dylan and Jaspal,



That works perfectly. thanks for your help!



Keith