Business rule to Auto populate manager user name based of manager id

sreedhar kota
Mega Guru

I want to write a business rule to populate employee Manager name on user ecord, Based on custom added field "manager id" (which is equal to employee number of manager). 

could anyone help on this with logic or code - Match manager id with employee number and update that user name under "manager". manager id is the employee number of manager user record. this manager id field is being updated by external script which is mapped with OKTA.

 

1 ACCEPTED SOLUTION

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

Use below business rule. After Insert Update with Condition as Manager Id Changes

 

Script:

 

var gr = new GlideRecord('sys_user');

gr.addQuery('employee_number',current.u_manager_id); //employee number is a field where employee id of users are stored and manager id is a field where manager id is stored.

gr.addActiveQuery();

gr.query();

if(gr.next())

{

current.manager = gr.sys_id;

current.update();

}


Thanks,
Ashutosh Munot

 

Mark answer as helpful or Correct

View solution in original post

4 REPLIES 4

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

Use below business rule. After Insert Update with Condition as Manager Id Changes

 

Script:

 

var gr = new GlideRecord('sys_user');

gr.addQuery('employee_number',current.u_manager_id); //employee number is a field where employee id of users are stored and manager id is a field where manager id is stored.

gr.addActiveQuery();

gr.query();

if(gr.next())

{

current.manager = gr.sys_id;

current.update();

}


Thanks,
Ashutosh Munot

 

Mark answer as helpful or Correct

This script looks pretty good, but there are a couple of things I would recommend changing.

1)  Eliminate 'current.update()' from the script.  This will almost always cause you issues with multiple updates running and triggering business rules multiple times.

2)  Run your business rule 'before' instead of 'after'.  This eliminates the need for the 'current.update()' line.

3)  Eliminate the 'addActiveQuery' line.  We just want to match the manager ID.  It shouldn't matter if the manager is active or not.

Thanks Mark for your inputs. i will embedded the suggested changes.

Thank you very much Ashutosh.

The script worked exactly well. much appreciated for your quick help