Set Manager name based on selected user

jesusnava
Giga Expert

Hello everyone,

Please I need your help, I have 2 fields one called "Requested for (requested_for) and another one named "Manager Name (manager_name)", The idea is that when the selected user changes, the manager changes as well, 

this is the script OnChange I am using but seems that something is not working: 

**************************************************************************

function onChange(control, oldValue, newValue, isLoading) {
if ((isLoading && !g_form.isNewRecord()) || (g_form.isLiveUpdating && g_form.isLiveUpdating()))
return;

if (newValue == '' || newValue == null) {
g_form.setValue('manager_name', '');
return;
}
if (!g_form.hasField('manager_name'))
return;
var requested_for = g_form.getReference('requested_for', setManagerName);
}

function setManagerName(requested_for) {
if (requested_for)
g_form.setValue('manager_name', requested_for.manager_name);
}

***************************************************************

Thank you!

1 ACCEPTED SOLUTION

Alexey7
Mega Sage

Hi,

You need a client-callable script include: 

var GetManagerName = Class.create();
GetManagerName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManagerName: function(){
var userRecord = new GlideRecord('sys_user');
userRecord.get(this.getParameter('sysparm_userID'));
return userRecord.manager.getDisplayValue();
},
type: 'GetManagerName'
});

 

And onChange client script for "requested_for" field:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading && !g_form.isNewRecord() || newValue == '' || newValue == oldValue) {
return;
}
var getManagerName = new GlideAjax('GetManagerName');
getManagerName.addParam('sysparm_name', 'getManagerName');
getManagerName.addParam('sysparm_userID', g_form.getValue('requested_for'));
getManagerName.getXML(populateManagerName);

function populateManagerName(response){
var nameFromScriptInclude = response.responseXML.documentElement.getAttribute('answer');
g_form.clearValue('manager_name');
g_form.setValue('manager_name',nameFromScriptInclude);
}
}

That should be it. Let us know if it works for you. Thank you.

View solution in original post

23 REPLIES 23

Hello Alexey,

I tried it the way you told me and works perfectly in servicenow platform, but when I try it in the Portal, the Manager shows when loading the form but once I change the user, the manager does not show:

This is when I load the form:

find_real_file.png

And this is when I change the user name:

find_real_file.png

And when I use the "Try it" button in servicenow, it works with no problem:

find_real_file.png

Am I doing something wrong?

Thanks!

Check "UI Type" on the client script, it should be set to "All" or whatever UI type you need. Let us know if it works. Thanks.

Excellent thank you!

The manager field seems to be a reference field, correct?  If you are going to use an Ajax call to retrieve information on a reference field, you are better off getting both the sys_id and the display value for that field so you can set it using g_form.setValue() with a third parameter (the display value).  This allows the system to display the value in the field/variable without having to go back to the server to get the display value, as it has to when only the sys_id is used.

Hi Jim, 

Could you modify the script I sent to jesusnava, so we can see what modifications need to be done to avoid the second request to the server?