I want to Inactive all the users who is having the ITIL Role by using the Background script

SreenadhChenna
Tera Contributor

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,

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

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

View solution in original post

4 REPLIES 4

Vishal Birajdar
Giga Sage

Hi @SreenadhChenna 

 

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.

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Danish Bhairag2
Tera Sage
Tera Sage

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

RAMANA MURTHY G
Mega Sage
Mega Sage

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

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

Siva Jyothi M
Mega Sage

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.