Remove the inactive members

siri27
Tera Contributor

In the group many of them are inactive, so two things to be done:
1. Remove the inactive members by running a fix script to fix the existing data
2. Fix the code which removes the inactive members from the group.

How to write fix script to remove the inactive members from the group.

3 REPLIES 3

RaghavSh
Kilo Patron

 

This code will remove inactive members from groups in fix script:

 

var usr = new GlideRecord('sys_user_grmember');
usr.addEncodedQuery('user.active=false');
usr.query();
while(usr.next())
{
usr.deleteRecord();
}

 


Raghav
MVP 2023

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @siri27 ,

You can use below code to remove inactive user from group.

 

var grpMember = new GlideRecord('sys_user_grmember');
grpMember.query();
while (ggrpMember.next()) {
    var gr = new GlideRecord('sys_user');
    gr.addEncodedQuery('active=false^sys_id=' +grpMember.user); // 
        gr.query();
        if (gr.next()) {
            grpMember.deleteRecord();
        }

    }

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

vikrantsharma
Kilo Guru

These scripts would help, however from an audit purpose, you shouldn't delete user records and it might trigger cascade deletion of the user references on the tickets. It is also a good practice for Audit purposes to keep the user and deactivate them.

Keep the user records, delete the group membership record.

Please make it correct or helpful if this solves or help you with your issue for other to make use it.

Thanks & Regards,
Vikrant Sharma