Script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 10:46 PM
I am currently working on a requirement where I need to populate the manager's name when a user is selected in the 'Assigned to' field on the Incident table. I have created a 'Manager' field as read-only, and I've developed a Script Include along with an OnChange Client Script to achieve this functionality. However, I'm facing issues with fetching and displaying the manager's name upon selecting the user in the 'Assigned to' field.
Could someone please assist me with the correct implementation? Additionally, I would like to know if this is the best approach. Specifically, I am interested in knowing which method would require less coding while maintaining efficiency.
Thank you in advance for your assistance. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2024 01:48 PM
It look like the other responses here may not work for a custom reference field to be set to the assigned_to user's manager. What works for that in my instance is a onChange client script defined for the assigned-to field with logic:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (oldValue == newValue) {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('GetUserManager');
ga.addParam('sysparm_name', 'getAssignedToManager');
ga.addParam('sysparm_userid', g_form.getValue("assigned_to"));
ga.getXMLAnswer(getResponse);
// callback function for returning the result from the script include
function getResponse(response) {
var man = response;
// alert('Manager: ' + man);
g_form.setValue('u_manager', man); // custom field is 'u_manager'
}
}
The script include has Client callable checked named "GetUserManager" with script:
var GetUserManager = Class.create();
GetUserManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssignedToManager: function() {
var userID = this.getParameter("sysparm_userid");
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.getValue();
// gs.info('GetUserManager: user manager = '+ result);
}
else {
result = "Unknown";
}
// gs.info("GetUserManager: Returning string: " + result);
return result;
},
type: 'XUserDetailsAjax'
});
Good luck on the approach you choose.