Making Users inactive requests received via catalog item

ServiceNow10sun
Giga Guru

Hi All, 

 

We get user inactivation request via catalog item , so looking for some suggestions if we can do it in bulk  the inactivation of  user accounts ,  users should automatically get removed from the groups , roles. 

Any suggestions is it possible via schedule jobs. 

 

Thank you in advance.

2 ACCEPTED SOLUTIONS

Claude DAmico
Kilo Sage

You could have a List Collector type variable so multiple users can be selected. Once submitted, set up an automation where needed to delete all sys_user_grmember and sys_user_has_role records where user.sys_idIN<list_collector_variable> kind of like below.

 

var userList = current.list_collector; //Get the list collector variable value here

var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user.sys_id', 'IN', userList);
grMem.query();
grMem.deleteMultiple();

var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('user.sys_id', 'IN', userList);
userRole.query();
userRole.deleteMultiple();

 

Claude E. D'Amico, III - CSA

View solution in original post

Sunny3008
Tera Guru

Hello @ServiceNow10sun ,

 

you can use the list_collector variable in your catalog item to get all the users that you want to deactivate, and add the following script in the workflow run script.

 

//To inactive Users
var grusr= new GlideRecord('sys_user');
grusr.addQuery('sys_id', 'IN',current.variables.{your variable name});
grusr.query();
grusr.active=false;
grusr.updateMultiple();

//To remove from group
var grpmbm = new GlideRecord('sys_user_grmember');
grpmbm.addQuery('user.sys_id', 'IN', current.variables.{your variable name});
grpmbm.query();
grpmbm.deleteMultiple();

//to remove roles of users
var usrl = new GlideRecord('sys_user_has_role');
usrl.addQuery('user.sys_id', 'IN', current.variables.{your varaible name});
usrl.query();
usrl.deleteMultiple();

 

 

 

Please mark this as the correct answer and helpful if it is resolved, or mark this helpful if this helps you to reach towards solution.

View solution in original post

3 REPLIES 3

Claude DAmico
Kilo Sage

You could have a List Collector type variable so multiple users can be selected. Once submitted, set up an automation where needed to delete all sys_user_grmember and sys_user_has_role records where user.sys_idIN<list_collector_variable> kind of like below.

 

var userList = current.list_collector; //Get the list collector variable value here

var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user.sys_id', 'IN', userList);
grMem.query();
grMem.deleteMultiple();

var userRole = new GlideRecord('sys_user_has_role');
userRole.addQuery('user.sys_id', 'IN', userList);
userRole.query();
userRole.deleteMultiple();

 

Claude E. D'Amico, III - CSA

Sunny3008
Tera Guru

Hello @ServiceNow10sun ,

 

you can use the list_collector variable in your catalog item to get all the users that you want to deactivate, and add the following script in the workflow run script.

 

//To inactive Users
var grusr= new GlideRecord('sys_user');
grusr.addQuery('sys_id', 'IN',current.variables.{your variable name});
grusr.query();
grusr.active=false;
grusr.updateMultiple();

//To remove from group
var grpmbm = new GlideRecord('sys_user_grmember');
grpmbm.addQuery('user.sys_id', 'IN', current.variables.{your variable name});
grpmbm.query();
grpmbm.deleteMultiple();

//to remove roles of users
var usrl = new GlideRecord('sys_user_has_role');
usrl.addQuery('user.sys_id', 'IN', current.variables.{your varaible name});
usrl.query();
usrl.deleteMultiple();

 

 

 

Please mark this as the correct answer and helpful if it is resolved, or mark this helpful if this helps you to reach towards solution.

Thank you its working as expected. However i have one query  as list collector is multiple selection that is array  and in the above script we are not declaring any array then how its still working even when i have selected multiple users in the list collector.