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

Dipali11
Tera Contributor

Hi i have filed name current_line_manager and it is reference field firstly  it set to default valeu that javascript:gs.getUser().getManagerName(); and when i removed it then it showing blank and this script also not working  what should i do ?

 

 

This post is marked as answered, so you may not get many responses.  You should post your own brand new question, referencing that you've try the script here (which one) and also post the code that you have tried already so we can see what might be wrong.

thanks jim 

amIT152
Tera Expert

Hello Jesu,

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);
}

 

With Regards,

amIT kumar