- 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 02:26 AM
Hello,
You can call your script include directly from within your u_hierarchique variable. Like:
Here is the script from the above Reference Qual:
javascript: (function () { if(current.variables.u_reprise_donnees == "No") { return "sys_idIN" + new UserUtils().getUserManager() }})();
Then your script include function looks like:
getUserManager: function() {
var userManager = "";
var userID = gs.getUserID();
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", userID.toString());
user.query();
if (user.next()) {
userManager = user.manager.sys_id.toString();
}
return userManager;
},
This works for me on my instance.
Hope this helps.
Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 12:39 AM
Hello,
I tried this but nothing have changed, even when I added logs in the Script Include's function, they didn't show.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 01:25 AM
Hi,
Can you double check that the function name in the script include matches the function name in the reference qualifier. Feel free to add screen shots of field variable / ref qualifier and script include.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-23-2022 05:01 AM
The problem was in my Script Include, it didn't contain this statement :
Object.extendsObject(AbstractAjaxProcessor, {
And also I had to remove the function initialize in the beginning of the Script.
Thank you

- 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.