Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

script to remove empty groups from user profile

Renu4
Tera Contributor

Hello Everyone,

After the upgrade of the version i noticed empty groups being added into a user profile which on expanding gives me sys_ids or alphanumeric numbers.

I want to fix this issue using backgroud or fix scripts.

Can somebody guide me?

Renu4_0-1685085015943.png

Renu4_1-1685085100521.png

i need to remove these in the red box via script for all the users.

Thank you in Advance,

Renu

1 ACCEPTED SOLUTION

Weird
Mega Sage

Well, the memberships themselves are stored in "sys_user_grmember" table.
You could do a query there and delete the extras. Here's a script that you can try first:

var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery("group.nameISEMPTY");
grMember.query();
gs.info("Amount of unnamed groups: " + grMember.getRowCount());
//grMember.deleteMultiple();

 Run this script in background and if the log text shows the same amount of rows as there are these incorrect group memberships, then you can uncomment the grMember.deleteMultiple(); and run it again to delete all those memberships.

Note that it might be a good idea to also add a grMember.setLimit(10) before the grMember.query() to test this out first with 10 records. You probably want to avoid deleting everything accidentally.

View solution in original post

2 REPLIES 2

Weird
Mega Sage

Well, the memberships themselves are stored in "sys_user_grmember" table.
You could do a query there and delete the extras. Here's a script that you can try first:

var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery("group.nameISEMPTY");
grMember.query();
gs.info("Amount of unnamed groups: " + grMember.getRowCount());
//grMember.deleteMultiple();

 Run this script in background and if the log text shows the same amount of rows as there are these incorrect group memberships, then you can uncomment the grMember.deleteMultiple(); and run it again to delete all those memberships.

Note that it might be a good idea to also add a grMember.setLimit(10) before the grMember.query() to test this out first with 10 records. You probably want to avoid deleting everything accidentally.

Ankur Bawiskar
Tera Patron
Tera Patron

@Renu4 

that related list table is for sys_user_group or sys_user_grmember

I believe it's for sys_user_grmember.

Also I believe the groups you are showing must be having name field as empty in their record. please check that

For sys_user_grmember there is no field marked as Display=true so in 2nd screenshot you are seeing sysIds.

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