auto populate manager name when user name is filled. (both are referenced to sys-user table)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 09:02 AM
can some please check this and provide a solution
function onChange(control, oldValue, newValue, isLoading) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 01:16 PM - edited 09-10-2024 07:23 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2024 02:34 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 12:01 AM
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 :
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 :
Output :
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2024 12:07 AM
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/