auto populate manager name when user name is filled. (both are referenced to sys-user table)

NavaneethK
Tera Contributor
I have tried this to pull the manager name from user name, but the manager name is blank for every user name.
can some please check this and provide a solution

function
onChange(control, oldValue, newValue, isLoading) {
    // Prevent the script from running during the initial form load
    if (isLoading || newValue === '') {
        return;
    }

    // Get the value of the user_name variable
    var userName = g_form.getValue('user_name'); // Replace 'user_name' with the actual variable name

    if (userName) {
        // Perform GlideAjax call to a Script Include to fetch the manager of the user
        var ga = new GlideAjax('GetUserManager');
        ga.addParam('sys_id', userName); // Send the sys_id of the user
        ga.getXMLAnswer(function(response) {
            var manager_Name = response;
            if (manager_Name) {
                // Set the manager_name variable with the manager's name
                g_form.setValue('manager_name', manager_Name); // Replace 'manager_name' with the actual variable name
            } else {
                // If no manager found, clear the field
                g_form.clearValue('manager_name');
            }
        });
    } else {
        // Clear manager_name if user_name is cleared
        g_form.clearValue('manager_name');
    }
}



var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getManagerName: function() {
        var userSysId = this.getParameter('sys_id');
        if (userSysId) {
            var userRecord = new GlideRecord('sys_user');
            if (userRecord.get(userSysId)) {
                var manager = userRecord.getValue('manager'); // Getting manager sys_id
                if (manager) {
                    var managerRecord = new GlideRecord('sys_user');
                    if (managerRecord.get(manager)) {
                        return managerRecord.getValue('name'); // Returning the manager's name
                    }
                }
            }
        }
        return ''; // If no manager found
    }
});
4 REPLIES 4

Bert_c1
Kilo Patron

Try the following client script logic:

 

 

 

 

  //Type appropriate comment here, and begin script below
   g_form.getValue("assigned_to");

	var ga = new GlideAjax('GetUserManager');
	ga.addParam('sysparm_name', 'getAssignedToManager');
	ga.addParam('sysparm_assignTo', g_form.getValue("assigned_to"));
	ga.getXMLAnswer(getResponse);

	// callback function for returning the result from the script include
	function getResponse(response) {
		alert('Manager: ' + response);
		 g_form.setValue('manager_name', response); // Replace 'manager_name' with the actual variable name
	}

 

 

 

in your onChange client script. the script include follows:

 

 

 

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getAssignedToManager: function() {
        var userID = this.getParameter("sysparm_assignTo");
		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id', userID);
		user.query();
//		gs.info("GetUserManager: Found " + user.getRowCount() + " user records.");
		if (user.next()) {
			result = user.manager.toString();		// sys-id value of the manager field
		}
		else {
			result = "Unknown";
		}
//		gs.info("GetUserManager: Returning string: " + result);
		return result;
    },

    type: 'XUserDetailsAjax'
});

 

 

 

 

Brad Bowman
Kilo Patron
Kilo Patron

If you are doing this in a Catalog Item and are on the Utah or later version, use the variable Auto populate feature instead.  Otherwise, what type of field is manager_name - reference or string?  You can add alerts to your client script and gs.info lines to your Script Include to see how far it is getting, the values passed between the two, record(s) returned by GlideRecord queries, script variable values, etc.  This way you will see where your error is.  It would also be helpful for troubleshooting to return something like 'not found' - if manager_name is a string/text field/variable, so that you know the scripts are actually running.

Amit Verma
Kilo Patron
Kilo Patron

Hi @NavaneethK 

 

You can make use of Auto-Populate feature for your requirement. Refer below steps :

 

1. Suppose I have two variables as shown below both referencing to sys_user table :

AmitVerma_0-1725865082557.png

 

2. All we need to do is to go to the manager variable, open Auto-Populate tab and make the Employee Name variable as dependent question followed by dot walk path to manager field as shown below :

AmitVerma_1-1725865192176.png

 

Output :

 

AmitVerma_2-1725865238911.png

 

AmitVerma_3-1725865252522.png

 

Thanks and Regards

Amit Verma


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

Ravi Gaurav
Giga Sage
Giga Sage

Hi @NavaneethK 

The issue might be with how the sys_id is being passed from the client-side script to the Script Include.

try Below Script :-

Client Script :-

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

var userName = g_form.getValue('user_name');

if (userName) {
var ga = new GlideAjax('GetUserManager');
ga.addParam('sysparm_name', 'getManagerName');
ga.addParam('sys_id', userName);
ga.getXMLAnswer(function(response) {
var manager_Name = response;
if (manager_Name) {
g_form.setValue('manager_name', manager_Name);
} else {
g_form.clearValue('manager_name');
}
});
} else {
g_form.clearValue('manager_name');
}
}

SI :-

var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManagerName: function() {
var userSysId = this.getParameter('sys_id');
if (userSysId) {
var userRecord = new GlideRecord('sys_user');
if (userRecord.get(userSysId)) {
var manager = userRecord.getValue('manager');
if (manager) {
var managerRecord = new GlideRecord('sys_user');
if (managerRecord.get(manager)) {
return managerRecord.getValue('name');
}
}
}
}
return '';
}
});

These corrections should solve the issue of the manager name not being populated.

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/