- 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-15-2019 06:22 AM
Sure. Here's a more "efficient" version(in terms of server calls).
Script Include:
var GetManagerName = Class.create();
GetManagerName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getManagerName: function(){
var result = {};
result.manager_id = "";
result.manager_name = "";
var userRecord = new GlideRecord("sys_user");
if (userRecord.get(this.getParameter("sysparm_user_id"))){
result.manager_id = userRecord.getValue("sys_id");
result.manager_name = userRecord.getValue("name");
}
return JSON.stringify(result); //send the object back as a JSON string
},
type: 'GetManagerName'
});
And then the Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
//clear the manager if the requestd for is cleared
if (newValue == "") {
g_form.setValue("manager_name", "");
}
var getManagerName = new GlideAjax("GetManagerName");
getManagerName.addParam("sysparm_name", "getManagerName");
getManagerName.addParam("sysparm_user_id", newValue); //no need for getValue as newValue already contains the info we need
getManagerName.getXMLAnswer(populateManagerName);
function populateManagerName(response){
var result = JSON.parse(response); //convert the JSON string back to an object
g_form.setValue("manager_name", result.manager_id, result.manager_name); //using a 3rd parameter will skip going back to the server to get the display value
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2019 07:02 AM
Jim, thank you for your input! I'm definitely going to use it.
Jesusnava, I highly recommend you to take a look at Jim's message. Moreover there is a mistake in the code I've provided, so you need to at least to get rid of the bolded part in the script include or it won't work correctly:
return userRecord.manager.getDisplayValue();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 10:28 AM
Thank you Alexey I will do so,
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 10:29 AM
Thank you I will try it this way,
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2024 10:16 PM
hi i tried this client script and script include but its not working