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

POOJA SINGH18
Mega Guru

Hi @levino 

 

In that case make use of Transform map.

 

Hope it helps and please mark helpful if it solves the issue.

Thanks,

Pooja Singh

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

Hi Rahul

var empID =  '882332,32253,235523';

is this the format?

 

encoded query should it be 'employee_numberIN

Thanks

Levino

 

Hi @levino ,
If you use 
var empID =  '882332,32253,235523';
with this you can directly use - 
'employee_numberIN'+empID;

 

 

and, If you use -
var empID =  ['882332','32253','235523'];

than you have to use 
'employee_numberIN'+empID.toString();

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