Adding role to multiple users
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2023 07:27 AM
Hi Experts,
we have 2 fields(it_owner & business_owner) in cmdb_ci_appl table.
the requirement is, we have to run a one time script to check and grant a role (xyz) to the users who are either it_owner or business_owner and don't have xyz role.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2023 07:30 AM
Hi @Puneet4418,
I'm curious, why a one time script? How are you handling changes to users in the fields?
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2023 07:33 AM
Hi Peter, currently I have written a BR for that.
This requirement is for the past entries.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2023 07:36 AM - edited ‎09-29-2023 07:37 AM
Hi @Puneet4418,
You can use the same script as in your BR, just place it in a loop going through your application records.
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2023 09:36 AM
@Puneet4418 Please use the following script as a part of a background script/fix script after making the role related changes and see if it works for you.
Caution: Please make sure to test this on a Sandbox environment and setting the Limit(1) initially. Once verified run for all the records.
var glideCIAppl = new GlideRecord('cmdb_ci_appl');
glideCIAppl.query();
while(glideCIAppl.next()){
if(!gs.nil(glideCIAppl.getValue('it_owner'))){
var itOwner = glideCIAppl.it_owner.getRefRecord();
if(!itOwner.hasRole('<add your role>')){
var sysUserRole = new GlideRecord('sys_user_has_role');
sysUserRole.initialize();
sysUserRole.setValue('user',glideCIAppl.getValue('it_owner'));
sysUserRole.setValue('role','42ffddsfff42332424fsdfdsfs'); //Add sys_id of your role here
sysUserRole.insert();
}
}
if(!gs.nil(glideCIAppl.getValue('business_owner'))){
var businessOwner = glideCIAppl.business_owner.getRefRecord();
if(!businessOwner.hasRole('<add your role>')){
var sysUserRole = new GlideRecord('sys_user_has_role');
sysUserRole.initialize();
sysUserRole.setValue('user',glideCIAppl.getValue('business_owner'));
sysUserRole.setValue('role','42ffddsfff42332424fsdfdsfs'); //Add sys_id of your role here
sysUserRole.insert();
}
}
}
Hope this helps.