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.

background script to copy members from one grp to other

kbanerje
Giga Guru

Hi,

I have two grps one newly created as permission type   grp(X) and another existing support type grp (Y).

The support type grp(Y) has 900 + users.   I need all the members of Y grp to be copied to X grp through background script.

Can any one help us with the background script for this?

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

var groupXid = 'sys_id_of_group_X';


var groupYid = 'sys_id_of_group_Y';


var newGr;


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group', groupYid);


gr.query();


while (gr.next()) {


  newGr = new GlideRecord('sys_user_grmember');


  newGr.initialize();


  newGr.group = groupXid;


  newGr.user = gr.user;


  newGr.insert();


}


View solution in original post

5 REPLIES 5

Geoffrey2
ServiceNow Employee
ServiceNow Employee

var groupXid = 'sys_id_of_group_X';


var groupYid = 'sys_id_of_group_Y';


var newGr;


var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group', groupYid);


gr.query();


while (gr.next()) {


  newGr = new GlideRecord('sys_user_grmember');


  newGr.initialize();


  newGr.group = groupXid;


  newGr.user = gr.user;


  newGr.insert();


}


Great minds code alike


Check out my Consultant's Survival Guide
https://youtube.com/watch?v=zYi8KhP9SUk

Hello, 

 

I've used this code and what it did for me was it duplicated the users from the source group.

I made minor modifications to be easier to for me to read.

 

var copy_to = 'sys_id of target group';
var copy_from = 'sys_id of source group'; 
var newGr;

 

var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group', copy_from);

gr.query();

while (gr.next()) {


newGr = new GlideRecord('sys_user_grmember');
newGr.initialize();
newGr.group = copy_to;
newGr.user = gr.user;
newGr.insert();


}

 

Are you able to help me check where failed?

 

Thank you.

John VanBruggen
Giga Guru

Here ya go.   Hope it helps,




//sys_id of the group you are copying from


var copy_from = '09851ff3c3123100918bb4ad81d3aea7';




//sys_id of the group you are copying to


var copy_to = '419d4913db0622009b4470adbf9619a2';


var gr = new GlideRecord("sys_user_grmember");


gr.addQuery("group", copy_from);


gr.query();


while (gr.next()) {


  gs.log(gr.user);


  var rec = new GlideRecord('sys_user_grmember');


  rec.initialize();


  rec.group = copy_to;


  rec.user = gr.user.sys_id;


  rec.insert();


}





Check out my Consultant's Survival Guide
https://youtube.com/watch?v=zYi8KhP9SUk