Need help with Script to add and remove users from group based on true/false field.

Daniel Shock
Kilo Sage

I have a checkbox on the user table to indicate whether or not a user is a manager.  I would like a business rule to maintain a group for managers by adding and deleting users from the group based on wether or not this box is checked.

The add script seems to be working correctly.  But the delete script is deleting users from any group they are a member of.  Here is the faulty script:

 

var gt = new GlideRecord('sys_user_grmember');


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


gt.addQuery('656d28f81386570031f25e7f3244b0be','sys_id_if_group');


gt.query();


while(gt.next())


{


gt.deleteRecord();


}

 

Help me to understand what I am doing wrong!

 

Thank you so much!

 

Daniel

1 ACCEPTED SOLUTION

Daniel,

Try with the below-edited one and let me know if it worked.

if(current.checkbox_name == true)
{
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group','sysid_of_managers_group');
grp.addQuery('user',current.sys_id);
grp.query();
if(!grp.next())
{
grp.initialize();
grp.group = sysid_of_managers_group;
grp.user = current.sys_id;
grp.insert();
}}
else
{
var grp1 = new GlideRecord('sys_user_grmember');
grp1.addQuery('group','sysid_of_managers_group');
grp1.addQuery('user',current.sys_id);
grp1.query();
if(grp1.next())
{
grp1.deleteRecord();
}
}

Thanks

View solution in original post

9 REPLIES 9

Hi Archana,

Any idea why this wouldn't work the same way on the HR profile instead of sys_user? Adding the user to the group works, but the deletion doesn't.

	if(current.u_organisation == 'Payroll'){
		var grpPay = gs.getProperty("sn_hr_core.payroll.group");
		var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user', current.user.sys_id);
		gr.addQuery('group', 'grpPay');
		gr.query();
		
		if(!gr.next()){
			
			gr.initialize();
			gr.group = grpPay;
			gr.user = current.user.sys_id;
			gr.insert();
			
		}
	} else {
		
		var gr1 = new GlideRecord('sys_user_grmember');
		gr1.addQuery('user', current.user.sys_id);
		gr1.addQuery('group', 'grpPay');
		gr1.query();
		
		if(gr1.next()){
			gr1.deleteRecord();
		}
	}

Hi,

I believe that you're doing something wrong here in the below line.

.addQuery('group', 'grpPay');

Try removing grpPay from quotations.

 

Thanks,

Archana

Thanks Archana, I figured it out. 

I needed to write the Business Rule in the Global Application scope, instead of HR.

Daniel Shock
Kilo Sage

Thank you so much for your help! Got it working

Glad to hear that!