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.

Move itil role users to new group using background script

nebiata
Tera Expert

I need to move about 600 users who has an itil role to a new group. What is the best way to do so?

1 ACCEPTED SOLUTION

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @nebiata ,

You can use fix script or background script.

For reference, you can use below script and update the sys_id of itil role and group at the respective lines.

var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role=282bf1fac6112285017366cb5f867469');//sys_id of the itil role
gr.query();
gs.info(gr.getRowCount());
while(gr.next()){
    var newGroup = new GlideRecord('sys_user_grmember');
    newGroup.initialize();
    newGroup.user = gr.user;
    newGroup.group = '019ad92ec7230010393d265c95c260dd' ; // sys_id of new group
    newGroup.insert();
}

Please Mark my answer Helpful & Accepted if I have answered your question.

Thanks,

Alka

View solution in original post

1 REPLY 1

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @nebiata ,

You can use fix script or background script.

For reference, you can use below script and update the sys_id of itil role and group at the respective lines.

var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role=282bf1fac6112285017366cb5f867469');//sys_id of the itil role
gr.query();
gs.info(gr.getRowCount());
while(gr.next()){
    var newGroup = new GlideRecord('sys_user_grmember');
    newGroup.initialize();
    newGroup.user = gr.user;
    newGroup.group = '019ad92ec7230010393d265c95c260dd' ; // sys_id of new group
    newGroup.insert();
}

Please Mark my answer Helpful & Accepted if I have answered your question.

Thanks,

Alka