- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 10:48 AM
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!
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 05:33 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 06:29 AM
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:
And this is when I change the user name:
And when I use the "Try it" button in servicenow, it works with no problem:
Am I doing something wrong?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 06:40 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 06:49 AM
Excellent thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 07:19 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2019 10:14 AM
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?