Deleting inactive users

I_Das
Tera Contributor

Hello all,

 

I want to remove inactive users from assignment group, I want to write a business rule on group member table.

 

When: before

 

Script:

(function executeRule(current, previous /*null when async*/) {
if (current.user) {
    var userGr = new GlideRecord('sys_user');
    if (userGr.get(current.user)) {
if (!userGr.active) {
            current.deleteRecord(); // Remove the user from the group
        }
 }
}
})(current, previous);
 
But it is not deleting the existing inactive users from the group. If you could tell me the issue in this it will be very helpful.
 
Thanks & Regards,
I Das
12 REPLIES 12

Gangadhar Ravi
Giga Sage
Giga Sage

@I_Das  Please check this 

 

https://www.servicenow.com/community/itsm-forum/how-to-remove-inactive-users-from-groups-and-revoke-...

 

Please mark my answer correct and helpful if this works for you.

Hello @Gangadhar Ravi 

 

As mentioned before I did try this instead of role I gave group and it worked. But it's only deleting the records when we update it like new user who was active before if we inactivate it it is working but the existing records which are there previously is not deleting. That's why I want to write BR in group member.

 

Thanks & Regards,

I Das

Eshwar Reddy
Kilo Sage

Hi @I_Das 

I believe it’s best to write a "BR" entry in the user table. This is important because if we mark a user record as inactive, the "BR" process will run and remove the user from the group.
Create BR on User Table 
Condition
active is false 

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', current.sys_id); 
gr.query();
while (gr.next()) {
gr.deleteRecord();
}

Please mark this response as Correct and Helpful if it helps you can mark more that one reply as accepted solution


Thanks
Eshwar

Hello @Eshwar Reddy ,

 

I did try and it worked. But it's only deleting the records when we update it like new user who was active before if we inactivate it it is working but the existing records which are there previously is not deleting. That's why I want to write BR in group member.

 

Thanks & Regards,

I Das