BR to set user 'active' field to false if member of a certain group

StewartFletcher
Tera Expert

We are looking to try and set users 'active' field to false if they are part of a certain group.

 

However, it appears the sys_user table cannot see the Groups related list, meaning I think we'd have to script that somehow.

 

Done a lot of scouring and cannot see anything that points to a solution. I see things about checking if a user is in a group, but not then 'do this'. I'm very much a scripting novice

 

So essentially if user is member of 'test group', then set Active to 'false'

14 REPLIES 14

Ankur Bawiskar
Tera Patron
Tera Patron

@StewartFletcher 

are you saying when user is added to particular group you want that user to be marked active false?

if yes then you will require after insert business rule on sys_user_grmember table

You need to be cautious since that user might be present in assigned to of tickets in your instance. If there won't be any tickets/cases assigned to those users then you are good.

Condition: Group [IS] Group ABC

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var rec = current.user.getRefRecord();
	rec.active = false;
	rec.update();

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi Ankur - this is 100% likely me not being clued up with scripting, but this doesn't seem to like that.

 

Does the condition need to be code too?

StewartFletcher_0-1688032747196.png

 

@StewartFletcher 

give that in the filter condition field

you can directly select Group [IS] <select the group>

AnkurBawiskar_0-1688033263859.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

That's worked perfectly, thank you.

 

Next thing I've just been asked - to reverse it, if we remove the user from the group, they stay as active = false. We want to change that to true - but creating another script to set that when the group changes from our 'test deactivate user' doesn't work. We have a Locked Out field that also sets, but seems we cannot reference that? Any thoughts