- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 01:29 AM
Hello,
I have a Catalog Item having a "Yes/No" variable "u_reprise_donnees" and another variable "u_hierarchique" which references "sys_user" table. If the variable "u_reprise_donnees" has the value "No", I want to fill the variable "u_hierarchique" automatically with the manager of the current user.
To do this, I have created an onChange Client Script on the variable "u_reprise_donnees" and if the new value of this variable is "No" then I call a function in the Script Include which will return the manager of the current user.
The problem is that the manager's value is showing Null and the logs in the Script Include are not showing.
Client Script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// If value of "u_reprise_donnees" is "Yes" then show message
if (newValue == "Yes") {
g_form.showFieldMsg("u_reprise_donnees", "La demande d'automatisation ne sera pas disponible dans le choix des changements urgents.");
}
//else if value is "No" then search for the manager id of the current user and assign it to "u_hierarchique" field
else if (newValue == "No") {
var gaUserUtils = new GlideAjax('UserUtils');
gaUserUtils.addParam('sysparm_name', 'getUserManager');
gaUserUtils.addParam('sysparm_user_id', g_user.userID);
gaUserUtils.getXML(ajaxResponse);
}
}
function ajaxResponse(response) {
var managerID = response.responseXML.documentElement.getAttribute("answer");
alert("Manager is : " + managerID);
g_form.setValue("u_hierarchique", managerID);
}
Script Include :
// Get the manager of the user
getUserManager: function() {
// Récupération des parametres
var userId = this.getParameter('sysparm_user_id');
gs.log("User is : " + userId, "Test");
var userManager;
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", userId);
user.query();
if (user.next()) {
userManager = user.getValue("manager");
}
gs.log("Manager is : " + userManager, "Test");
return userManager;
},
I really appreciate any help you can provide.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 02:49 AM
Hi WA,
Tried both Client Script and Script Include and they are working OK.
I've delete the onLoading if statement in Client Script so it'll execute when the form is loaded. I, also, already had Script Include named UserUtils2 so I've named my Script Include "UserUtils2".
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') { // remove onLoading
return;
}
// If value of "u_reprise_donnees" is "Yes" then show message
if (newValue == "Yes") {
g_form.showFieldMsg("u_reprise_donnees", "La demande d'automatisation ne sera pas disponible dans le choix des changements urgents.");
}
//else if value is "No" then search for the manager id of the current user and assign it to "u_hierarchique" field
else if (newValue == "No") {
var gaUserUtils = new GlideAjax('UserUtils2'); // renamed from UserUtils
gaUserUtils.addParam('sysparm_name', 'getUserManager');
gaUserUtils.addParam('sysparm_user_id', g_user.userID);
gaUserUtils.getXML(ajaxResponse);
}
}
function ajaxResponse(response) {
var managerID = response.responseXML.documentElement.getAttribute("answer");
alert("Manager is : " + managerID);
g_form.setValue("u_hierarchique", managerID);
}
Execution
case 1: when form is opened.
case 2: when u_reprise_donnees is set to No
If it is not working, it may be with Script Include settings. Make sure "Client callable" is checked. This will generate the following script without "initialize: function() {}," statement.
var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
Also, name of the file "UserUtils" should be set in all the the script as follows.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 01:38 AM
Hi,
As best I recall you cannot call (server side) Script Includes from Client Scripts, you would need to use a UI Script (sys_ui_script).
hth,matt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 01:47 AM
Can try changing the variable name in Script Include from user to userGr something. I feel it is a keyword.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 01:49 AM
Also check if manager is actually populated in sys_user table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2022 12:38 AM
Hello,
I have changed the variable name and I checked that the manager of the current user already exist but this didn't solve the problem..