The CreatorCon Call for Content is officially open! Get started here.

Adding roles in bulk

jared_wentzel
Kilo Contributor

I'm trying to add a role to all users who have a certain value for company. So I found the following wiki page script and I am trying to modify it but first of all I'm not sure where to put it for a one time on demand run. I trying putting in in a scheduled job, and a client script with no dice...

http://wiki.servicenow.com/index.php?title=Useful_User_Scripts

And here is my script...

var gr = new GlideRecord("sys_user");

gr.addQuery('company', 'Fulbright & Jaworski L.L.P.');

gr.query();

while(gr.next()) {

  if (gr.accumulated_roles.toString().indexOf(",us_users,") == -1) {

    gr.roles = gr.roles + ",us_users";

    gr.update();

}

1 ACCEPTED SOLUTION

company is a related record on the user form so you will need to use the sys_id of the company



change


gr.addQuery('company', 'Fulbright & Jaworski L.L.P.');




to




gr.addQuery('company', 'sys_id of the company');


View solution in original post

20 REPLIES 20

ohhgr
Kilo Sage

Hi Jared,



You could put that code in an On Demand scheduled job and run it once.



However, if it is going to be run only once, you could do so by elevating the privileges and running the background script.




Thanks,
Mandar


I already attempted the On Demand scheduled job approach, but that had not worked, What is the second thing you are mentioning? How do I run a background script?


acretion
Tera Expert

try something like this in the scheduled job instead



var gr = new GlideRecord("sys_user");


gr.addQuery('company', 'Fulbright & Jaworski L.L.P.');


gr.query();


while(gr.next()) {


  var myrole = new GlideRecord('sys_user_has_role');


  myrole.user = gr.sys_id;


  myrole.role = '0d161bdd745155000928dcfea8562f5a'; // change this to be the sys_id of your role


  myrole.insert();


}


How do I get the sysid of the role