- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2016 04:24 PM
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();
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2016 05:52 PM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2016 04:41 PM
There is a field called Accumulated Roles on the user record which is of type string.
So in this line if (gr.accumulated_roles.toString().indexOf(",self_service,") == -1) {
it check if the that field has self_service role if not then they are adding that role to the accumulated_roles field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2016 04:54 PM
I copied the entire code and running in background script, but it not adding self_service role to the users?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2016 04:58 PM
In OOB only the users with maint role has read and write access.
If you want to test it then in ACL's disable the read and write access on sys_user.accumulated_roles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2016 05:23 PM
Ok, I disabled the above acls I copied and paste the above script in the background script, executed it, then I open the user table, abel tutor and opened the roles
and donot see self_service roles in there?