Call of Script Include in an onChange Client Script is not working

WA1
Kilo Sage

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);
}

find_real_file.png

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;
    },

find_real_file.png

I really appreciate any help you can provide.

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

find_real_file.png

case 2: when u_reprise_donnees is set to No

find_real_file.png

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. 

find_real_file.png

 

View solution in original post

10 REPLIES 10

Matt102
Giga Guru

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

suvro
Mega Sage
Mega Sage

Can try changing the variable name in Script Include from user to userGr something. I feel it is a keyword.

Also check if manager is actually populated in sys_user table

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