adding roles to user

sunnysunny
Kilo Contributor

This is the script for adding roles to the user, which I copied from wiki. Can anyone explain what is accumulated_roles here and why we convert this to string?

var gr = new GlideRecord("sys_user");

gr.query();

while(gr.next()) {

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

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

  gr.update();

}

}

1 ACCEPTED SOLUTION

Here is the script that   will add self service role to all the active users




var gr = new GlideRecord("sys_user");


gr.addQuery('active', true)


gr.query();




while(gr.next()) {




      var grd = new GlideRecord('sys_user_has_role');


              grd.user = gr.sys_id;


grd.role = '[sys_id of the role you want to add]'; // sys_id of self_service role


grd.insert();




}

View solution in original post

13 REPLIES 13

Hey sorry. above script in updating the Roles field not the accumulated roles..


sunnysunny
Kilo Contributor

Ok I need to add self_service role to all the users, so how can I do that?


Can you explain us the business reason a little bit?


So that i can tell you if you need a script to achieve that


sunnysunny
Kilo Contributor

they want me to add the self_service role to the role table, so that when we edit the role, we can see that value in there


In order to add a role go to roles module and click new and enter the name.


find_real_file.png



Once you save the record sys id is created. To get the sys id of the record right click on the form header then you should see options like below


find_real_file.png