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

jared_wentzel
Kilo Contributor

Ahhh that was it! I had to use the sys_id for the company value... Geez it's always the little things that will trip you up! Thank you!!


jared_wentzel
Kilo Contributor

The script I'm using works and I have it as a scheduled job but I realized I keeps on adding the roles over and over again so one user will have the same role multiple times. So I need to add a if statement to it but when I came up with isn't working.. Any ideas?



var gr = new GlideRecord("sys_user");



gr.addQuery('company', '818c19b66fff55003ac289871e3ee4b1');



gr.query();



while(gr.next()) {


     


  if (gr.accumulated_roles.toString().indexOf(",us_users,") == -1) { // IF statement added that it not working


         


  var myrole = new GlideRecord('sys_user_has_role');



  myrole.user = gr.sys_id;



  myrole.role = 'b1e8e21b643521006601c2c80d6c04bf';



  myrole.insert();


  }


}


I normally do a query to see if it exists already...



    var myrole= new GlideRecord("sys_user_has_role");


          myrole.addQuery("user", gr.sys_id);


          myrole.addQuery("role", 'b1e8e21b643521006601c2c80d6c04bf');


          myrole.query();


          if (myrole.next()) {


                gs.log(" already has the group_admin role for the group - not adding");


          } else {


                myrole.initialize();


                myrole.user = gr.sys_id;


                myrole.role = 'b1e8e21b643521006601c2c80d6c04bf;


 


                myrole.insert();


                      }


jared_wentzel
Kilo Contributor

The code you're showing doesn't include the initial logic I need to have for the company field on the sys_user table. I don't want to add this role to everyone..



So basically I would need to have both conditions, if the users company is '818c19b66fff55003ac289871e3ee4b1' and if they do not already have the role.


yes sorry, just wanted to show you the solution is to query for the record, if it exists don't create it again.