- 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-13-2019 10:50 AM
Wat are you trying to achieve here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 10:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 11:00 AM
Hi,
okay for this write an on change client script
var r=g_form.getReference('requested_for');
g_form.setValue('manager_name',r.manager);
try the above code and if find any problem post it
Regards
Ravindra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2019 10:58 AM
It's always recommended to use getReference() with callback function, this runs asynchronously, and script processing will continue normally until the server returns the reference value, at which time the callback function will be invoked. Please find more details here: GlideForm (g form) - ServiceNow Wiki
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var reqFor = g_form.getReference('<field name of requested for>', doAlert);
function doAlert(reqFor) {
g_form.setValue('<field name of manager>', reqFor.manager);
}
}
Hope this helps.
Let me know if you need more help.
Thanks,
Rajashekhar Mushke
Community Leader
Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke