How to get manager email for a logged-in user using javascript?

runfast
Kilo Guru

I am trying to populate a form with the logged in user manager email.   I can get the manager name using the manager ID but I am have a little bit of challenge getting the manager email address.

I found this article to get manager of manager. I tried to tweak it without any luck.

How can you get manager of manage of logged-in user as default value?

gs.getUser().getUserByID(gs.getUser().getManagerID()).getManagerID();

Any guidance or direction greatly appreciated.

thanks

Alex

2 ACCEPTED SOLUTIONS

christopheriron
Tera Expert

Hi Alex,



You will need to call a glide record for this, the below should work.



var manager = new GlideRecord('sys_user');


manager.get(gs.getUser().managerID);



manager.email



Thanks.


View solution in original post

var manager = new GlideRecord('sys_user');


manager.get(gs.getUser().managerID);


manager_email=manager.email;


View solution in original post

12 REPLIES 12

var manager = new GlideRecord('sys_user');


manager.get(gs.getUser().managerID);


manager_email=manager.email;


Hi Alex,



I haven't come across the getManagerID() method before but the below script will work:



var user = new GlideRecord('sys_user');   //glide into the user table


if(user.get(gs.getUserID())){                               //get the record with the same sys_id as the currently logged in user


var managerMail = user.manager.email.getValue(); //get the value of the managers email


current.field_on_your_form = managerMail;   //populate your field


}


If you're using a string field type then default value should work with the above including manager.email on the last line. You will need to add javascript: before the first bit of code to get it to function correctly but the value will be the last output from the script.



Thanks


Hey guys, I am not getting desired results with above script.

 

I created a  reference field on sys_user table, and put the below code in Default value on the form field named "Supervisor email address:

find_real_file.png

var user = new GlideRecord('sys_user');   //glide into the user table

if(user.get(gs.getUserID())){                               //get the record with the same sys_id as the currently logged in user
var managerMail = user.manager.email.getValue(); //get the value of the managers email
current.Supervisor_email_address = manager.email;   //populate your field
}

You can't put code in the default value section on the dictionary entry. You'll need to use a business rule to set the value of your reference field.

It also looks like you've mixed up my code with that from chris and runfast and it also looks like the field name might be incorrect (unless this is a service catalog variable?)  Last line should be:

 

current.u_supervisor_email_address = managerMail;  //check the field name
current.variables.Supervisor_email_address = managerMail;  //if service catalog variable