Adding role to multiple users

Puneet4418
Tera Contributor

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.

4 REPLIES 4

Peter Bodelier
Giga Sage

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.

Hi Peter, currently I have written a BR for that.

This requirement is for the past entries.

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.

Sandeep Rajput
Tera Patron
Tera Patron

@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.