The CreatorCon Call for Content is officially open! Get started here.

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

Hi Mason,

As suggested by Muhammad you need to remove those users from the groups and automatically roles would get deleted

Regards
Ankur

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

So did you had a look at what I mentioned 2 days ago? Removing the groups relationship?

Code = tested. Obviously you could/should test yourself with a setLimit first.

(function() {

	var grUser = new GlideRecord('sys_user_grmember');
	grUser.addEncodedQuery('user.active=false');
	grUser.addEncodedQuery('user.locked_out=true');
	grUser.setLimit(10);

	grUser.deleteMultiple();

})();

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 there,

Did this solve your question? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

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 there,

Did this solve your question? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

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

Mark Roethof
Tera Patron
Tera Patron

For example, just tested below code, works fine with admin role. Just update the encodedQuery. Do note what I mentioned, you might also want to look at the group relation.

(function() {

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

	grUser.deleteMultiple();

})();

If performing this in a background script or fix script, do wrap the code inside a function. This is best practice.

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