How to get current loggedin user manager name and his email id?

manmathpanda
Giga Guru

How to get current loggedin user's manager name and email id?

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

If it's absolutely necessary to create a client script, following will get current user's manager's info.

Client script

function onLoad() {
    var ajax = new GlideAjax('UserUtilClient');
    ajax.addParam('sysparm_name', 'getManagerInfo');
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            var json = JSON.parse(answer);
            g_form.setValue('manager_name', json.name);
            g_form.setValue('manager_email', json.email);
        }
    });
}

Script include

var UserUtilClient = Class.create();
UserUtilClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManagerInfo: function() {
        var grUser = new GlideRecord('sys_user');
        if (grUser.get(gs.getUserID())) {
            var manager = grUser.manager;
            if (manager) {
                return JSON.stringify({
                    "name": manager.name.toString(),
                    "email": manager.email.toString()
                });
            }
        }
    },
    type: 'UserUtilClient'
});

The result is a same as when using default value. This is a little bit slower because the form is loaded and then the form makes an ajax request to the server to get manager's information. When setting the default value, manager's name and email is filled when before the form is loaded so there's no ajax call after the form is loaded.

find_real_file.png

View solution in original post

10 REPLIES 10

Upender Kumar
Mega Sage

Hi,

Use

 

var currentUser = gs.getUserID();
var gr=new GlideRecord('sys_user');
gr.get(currentUser);
gs.log(gr.email);

sachin_namjoshi
Kilo Patron
Kilo Patron

Please use below in your script to get current user's manager and email

 

 

var user = new GlideRecord('sys_user');

user.get(gs.getUserID());


gs.print(user.manager.name);

gs.print(user.manager.email);

 

Regards,

Sachin

 

bernyalvarado
Mega Sage

Here goes a simple way to do it 🙂 

 var email = gs.getUser().getEmail(); //This will retrieve the email

var managerID = gs.getUser().getRecord.getValue('manager'); //This will retrieve the Manager ID (sys_id). 

 If you're recording for logging/audit purposes the manager or if you want to use it for other operations, using the sys_id might be a more robust way to approach it than using the name. If you still want the name of the manager... you can do something like...

var userGR = new GlideRecord('sys_user');

userGR.get (gs.getUser().getRecord.getValue('manager'));

var managerName = userGR.name;

I hope it helps! 🙂

Thanks,

Berny

 

Allen Andreas
Administrator
Administrator

Hi,

The real question is, how are you planning to do this? BR? Client Script, etc?

Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!