Removing Roles of Inactive users

Mason5
Mega Expert

Hi All,

I am trying to remove all roles of users(from sys_user_has_role) who are inactive and locked out in user table but it does not allow to remove records from sys_user_has_role table, does not delete with Background script as well.

How can we achieve this, any idea?

Thanks in Advance!

1 ACCEPTED SOLUTION

Hi Mason,

ensure you wrap code inside function

updateRecord();

function updateRecord(){

try{

var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('user.locked_out=true^user.active=false');

gs.info('Row Count is: ' + gr.getRowCount());

gr.query();
while(gr.next())
{
gr.deleteRecord();
}
}

catch(ex){
gs.info('Exception is: ' + ex);
}

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

21 REPLIES 21

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What do you mean with "it does not allow to remove records"? Can you share an example. Or might this be inherited roles perhaps (are the group relations removed also)?

Deleting records from this table should be possible.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

There is not "Delete" option in list choice, most of them are inherited roles from groups but there are 100+ different groups.

 

Do you have steps to reproduce on how you are trying to do this? Out-of-the-box I just have a Delete UI Action.

Did this got changed in your instance? ACL's? What role are you using? Etc..

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

So did you actually try to remove those users from those groups? With that, also the inherited roles will be removed.

Almost same query:

(function() {

	var grUser = new GlideRecord('sys_user_grmember');
	grUser.addEncodedQuery('user.active=false');

	grUser.deleteMultiple();

})();

(do add a setLimit for testing purposes!)

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn