Removing users from a group

prernabhandari
ServiceNow Employee
ServiceNow Employee

Hello All.

I have a requirement where I need to remove around 100 users from the a group. The Only way I know is editing the group Members/editing a   collection list.

This becomes a manual process. Is there any other method I can use? I am thinking in lines of a fix script.

TIA

Prerna

10 REPLIES 10

There are probably a lot of ways to achieve this. One way would be to use the "load data" feature, which will allow you to load the CSV. Once the spreadsheet is loaded into a table you can then execute your script against it (you could use "Scripts - Background" for instance, which requires elevated permissions to run).



In Scripts - Background you have complete flexibility to do what you want - so by loading the data into a table you can cycle through the users, relate them to your user table, then perform whatever functions you want.


I agree with Ismail.



Your code will depend on XLS and that is why i am emphasizing more on data validity of it.



If you have group name available and user name available your modified code would be



var target = new GlideRecord('sys_user_grmember ]


target.addQuery('group.name', 'name of the group you have in XLS);


target.addQuery('user.sys_user_field', 'linking value');


target.query();


ismailsn
ServiceNow Employee
ServiceNow Employee

Is there something specific about these 100 users that you need to remove from the group? Are they the only ones in this group you want to remove users from? As Trevor said, Deepak said- there is a [ sys_user_grmember ] table that contains the members of each group.



You can easily write a GlideScript that will take care of this:



var target = new GlideRecord('sys_user_grmember ]


target.addQuery('group', 'group_sys_id');


target.addQuery('user.sys_user_field', 'linking value');


target.query();


while(traget.next()){



gs.print("We are deleting user: " + target.user.getDisplayValue() + " From the group: " + target.group.getDisplayValue());



target.deleteRecord();



}



Please note this is pseudo code and will have to be modified to meet your criteria.



Regards


Hello Prerna,



If my understanding is correct with reference to your requirement we can also, fulfill by approach of transform maps in data sources & importing data in the instance. Instead of scripting.



if you want   any info please let us know.



Thanks,


Surya Kattela (KSN Swamy).


Hi Reddy,



Can you please elaborate how to remove users using transform maps ?



I am able to add the users to groups using the transform map but not remove them automatically.



Please note that we get the data in the form of an excel file.



You help is highly appreciated.



-Raghu