- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 04:01 PM
Hi,
I want to Inactive all the users who is having the ITIL Role. i want to fulfill this requirement by using the Background script in Servicenow.
Kindly help me out with the script.
Thanks,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 09:50 PM
Hi @SreenadhChenna ,
In order to inactivate users containing 'ITIL' role you can refer below code.
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('role','itil');
gr.query();
while(gr.next()){
var deactivate = new GlideRecord('sys_user');
deactivate.addQuery('sys_id',gr.user);
deactivate.query();
if(deactivate.next()){
deactivate.active = false;
deactivate.update()
}
}
Please test in lower environment before executing in Prod.
Also please mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 07:25 PM
var grRole = new GlideRecord('sys_user_role'); // roles table
grRole.addQuery('role','ITIL');
grRole.query();
while (grRole.next()) {
var grUser = new GlideRecord('sys_user');
grUser.addQuery('user',grRole.user);
grUser.query();
if (grUser.next(){
grUser.active = false;
grUser.update();
}
}
You can use this code as reference, change the backend value of fields if needed
Also, to test limit the query so not all records will update. After testing if you think code is working then only run the full query.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 09:50 PM
Hi @SreenadhChenna ,
In order to inactivate users containing 'ITIL' role you can refer below code.
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('role','itil');
gr.query();
while(gr.next()){
var deactivate = new GlideRecord('sys_user');
deactivate.addQuery('sys_id',gr.user);
deactivate.query();
if(deactivate.next()){
deactivate.active = false;
deactivate.update()
}
}
Please test in lower environment before executing in Prod.
Also please mark my answer helpful & accepted if it helps you resolve your issue.
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 09:56 PM - edited 09-25-2023 09:59 PM
Hello @SreenadhChenna ,
Please try below code
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery('roles=ITIL');
userGr.query();
while (userGr.next()) {
userGr.active = true;
userGr.update();
}
gs.info('Deactivated ' + userGr.getRowCount() + ' users with ITIL role.');
Please mark my answer correct and helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 10:03 PM
Hi @SreenadhChenna,
var ga= new GlideRecord('sys_user_has_role'); //user role table
ga.addQuery('role','itil');// quering the itil role users
ga.query();
while(ga.next()){
var gausers= new GlideRecord('sys_user');
gausers.addQuery('sys_id',ga.user);
gausers.query();
while(gausers.next()){
gausers.active = false;
gausers.update();
}
}
Please mark this answer as correct and helpful if it solves your issue.
Regards,
Siva Jyothi M.