How to assign any role automatically depends on a field

shaik_irfan
Tera Guru

Hello Everyone,

I have a requirement where i need to grant ITIL roles to the users who has the Title as Manager.

We have a field called Title   in User table if the field starts with Manager for them ITIL roles to be granted automatically.

can anyone please help me out regarding to this ?

1 ACCEPTED SOLUTION

Simon46
ServiceNow Employee
ServiceNow Employee

Hello Shaik,



The following line checks if the user has the role "self_service" and a "-1" means the user does NOT (value not found).


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


If you want to add a user to a group, the code is a little bit more complex as you would need to utilise "GlideRecord". Don't forget to check if the user is already part of the group before adding them in.



  1. //Create a new group relationship record for this user  
  2. var rec1 = new GlideRecord('sys_user_grmember');
  3. rec1.initialize();
  4. rec1.user = current.sys_id;
  5. rec1.group.setDisplayValue('Asia Group');
  6. rec1.insert();


Kind regards,


Simon


View solution in original post

6 REPLIES 6

Simon46
ServiceNow Employee
ServiceNow Employee

Hello Shaik,



You could achieve this is a Business Rule on the [sys_user] table.



Using the "current" variable, check if the field "Title" starts with "manager" then grant the "ITIL" role using a script.



Can you refer to the following documentation on how to write the script for adding the role.


Add role to every user



Kind regards,


Simon


Thanks Simon,



That helps



Can you please explain me below line ?



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



And let me add 1 more point we created a Group named as ITIL can i use below script if i want to add group name instead of role name ?



gr.group= gr.group+ ",ITIL"; 




Simon46
ServiceNow Employee
ServiceNow Employee

Hello Shaik,



The following line checks if the user has the role "self_service" and a "-1" means the user does NOT (value not found).


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


If you want to add a user to a group, the code is a little bit more complex as you would need to utilise "GlideRecord". Don't forget to check if the user is already part of the group before adding them in.



  1. //Create a new group relationship record for this user  
  2. var rec1 = new GlideRecord('sys_user_grmember');
  3. rec1.initialize();
  4. rec1.user = current.sys_id;
  5. rec1.group.setDisplayValue('Asia Group');
  6. rec1.insert();


Kind regards,


Simon


Simon,



it helped me but i have couple of questions,


1: which type do you think is good is it Before BR or After BR


2: If i want to run this script on existing user will it works ?