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.

add members to a Group, via background script

levino
Giga Guru

Hi Experts

i have a  list of  employee id attributes which are in the sys_user table

e.g.  employee number 24424,4423,63346,52525,252552 etc

i need to add these users to the group.

 

i could write a encoded query but since list of enployee number is huge, if there is way to do it via a fix script or background script

 

Thanks

Levino

1 ACCEPTED SOLUTION

Rahul Talreja
Mega Sage

Hi @levino ,

Do you have a array of empIDs ready?

You can use this!

 

var empID = [<emp I'd of all empyin string>];
var gr= new GlideRecord('sys_user');
gr.addEncodedQuery('emp_idIN'+empID.toString());
while(gr.next()){
var grmem = new GlideRecord('sys_user_grmember');
grmem.initialize();
grmem.group = 'sys id of group';
grmem.user = gr.sys_id;
grmem.insert();
}

 

If you are looking for something else, please let me know.

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hi @levino ,

var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group=current.variables.variable_name_group;//field name or the object where group sys_id is stored on the form
gr.user = current.variables.variable_name_user;//field name or the object where user sys_id is stored on the form.
gr.insert();