script include

mahesh009
Mega Guru

How can I create a custom script include to retrieve the name of the current user's manager in ServiceNow?

1 ACCEPTED SOLUTION

VishaalRanS
Tera Guru

Hi @mahesh009 

 

Try this custom script include (To create in ServiceNow to retrieve the name of the current user's manager).

 

var MyManager = Class.create();
MyManager.prototype = {
    initialize: function() {
    },
    getManagerName: function() {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', gs.getUserID());
        gr.query();
        if (gr.next()) {
            var managerID = gr.manager.sys_id;
            if (managerID) {
                var managerGr = new GlideRecord('sys_user');
                managerGr.addQuery('sys_id', managerID);
                managerGr.query();

                if (managerGr.next()) {
                    return managerGr.first_name + ' ' + managerGr.last_name;
                }
            }
        }

        return 'No manager found';
    }
};
 

 

To use this script include, you can call the getManagerName() method from your business rules, client scripts, or other ServiceNow components. For example, in a business rule:

 

var managerName = new MyManager().getManagerName();
gr.setValue('manager_name', managerName);

 

 This script include first retrieves the current user's sys_id. Then, it queries the sys_user table to find the manager's sys_id. Finally, it queries the sys_user table again to retrieve the manager's name. If no manager is found, the script returns "No manager found."

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

4 REPLIES 4

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @mahesh009 

Refer the below thread:

https://www.servicenow.com/community/developer-forum/how-to-get-current-loggedin-user-manager-name-a...

 

Please mark this answer as helpful and correct if helped

 

Kind Regards,

Ravi

Omkar Mone
Mega Sage

You can do it with this 

 

var managerID = gs.getUser().getManagerID();

 

also refer to https://servicenowguru.com/scripting/user-object-cheat-sheet/ in case you need another method to retrieve user related data without much scripting.

VishaalRanS
Tera Guru

Hi @mahesh009 

 

Try this custom script include (To create in ServiceNow to retrieve the name of the current user's manager).

 

var MyManager = Class.create();
MyManager.prototype = {
    initialize: function() {
    },
    getManagerName: function() {
        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', gs.getUserID());
        gr.query();
        if (gr.next()) {
            var managerID = gr.manager.sys_id;
            if (managerID) {
                var managerGr = new GlideRecord('sys_user');
                managerGr.addQuery('sys_id', managerID);
                managerGr.query();

                if (managerGr.next()) {
                    return managerGr.first_name + ' ' + managerGr.last_name;
                }
            }
        }

        return 'No manager found';
    }
};
 

 

To use this script include, you can call the getManagerName() method from your business rules, client scripts, or other ServiceNow components. For example, in a business rule:

 

var managerName = new MyManager().getManagerName();
gr.setValue('manager_name', managerName);

 

 This script include first retrieves the current user's sys_id. Then, it queries the sys_user table to find the manager's sys_id. Finally, it queries the sys_user table again to retrieve the manager's name. If no manager is found, the script returns "No manager found."

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

brahmandlapally
Mega Guru

Hi @mahesh009 

Refer the below link

https://www.servicenow.com/community/developer-forum/how-to-get-current-loggedin-user-manager-name-a...

 

use GlideRecord API and query the logged in user and retrieve the manage name.

Thank you.