Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix script to delete roles, groups assigned to existing user who is inactive.

Kavya Policepa1
Giga Contributor

I want Fix script to delete roles, groups assigned to existing user who is inactive in sys_user table.

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

hello @Kavya Policepatil ,

try this 

var inusr = new GlideRecord('sys_user');
inusr.addQuery('active','false');
inusr.query();
while(inusr.next())
{
var role = new GlideRecord('sys_user_has_role');
role.addQuery('user',inusr.sys_id);
role.query();
while(role.next())
{
role.deleteRecord();
}
var group = new GlideRecord('sys_user_grmember');
group.addQuery('user',inusr.sys_id);
group.query();
while(group.next())
{
group.deleteRecord();
}

}

But usually once you make a person in active in user table automatically he /she will be removed from the groups table .

But yeah give it a try 

please mark my answer correct if it helps you

View solution in original post

4 REPLIES 4

Mohith Devatte
Tera Sage
Tera Sage

hello @Kavya Policepatil ,

try this 

var inusr = new GlideRecord('sys_user');
inusr.addQuery('active','false');
inusr.query();
while(inusr.next())
{
var role = new GlideRecord('sys_user_has_role');
role.addQuery('user',inusr.sys_id);
role.query();
while(role.next())
{
role.deleteRecord();
}
var group = new GlideRecord('sys_user_grmember');
group.addQuery('user',inusr.sys_id);
group.query();
while(group.next())
{
group.deleteRecord();
}

}

But usually once you make a person in active in user table automatically he /she will be removed from the groups table .

But yeah give it a try 

please mark my answer correct if it helps you

group.deleteRecord()

semi colon missing šŸ™‚

 

Regards,
Sumanth

yep ! thanks sumanth edited it 

Thank you.