Need a script to add a user role in sys_user table

Saib1
Tera Guru

Hi ,

 

Need a script to add a user role in sys_user table

4 REPLIES 4

Veer
Tera Guru

@Saib1  you can not insert user role in sys_user table, user & role relationships are stored in sys_user_has_role table.

 

 

var gr = new GlideRecord('sys_user_has_role');
gr.initialize();
gr.user = ''; // within quotes add sys_id of the user.
gr.role = ''; //within quotes add sys_id of the role.
gr.insert();

 

Kashyap G
Tera Expert

Hi @Saib1 

 

Please use the following script:

 

var role= new GlideRecord("sys_user_has_role");
role.initialize();
role.setDisplayValue('user','{Name of the user}');
role.setDisplayValue('role','{Name of the role]');
role.state="Active";
role.insert();
 
Hope this helps.
 
*Please mark this as helpful based on the impact.*

BR,
Kashyap G 

@Kashyap G 

 

If we want to add Role to Group ? what will be the script?

If we want to remove a Role from Group ? What will be the script?

dhruvd
Tera Expert

Hi @Saib1 ,

 

Try the code below :-

 

(function() {

var grUserRole = new GlideRecord('sys_user_has_role');
grUserRole.initialize();
grUserRole.user = ''; // sys_id of user record.
grUserRole.role = ''; // sys_id of role record.
grUserRole.insert();

})();

 

Thanks,
Dhruv