Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

GlideAjax response returns null on client script

venkatsaladi
Tera Contributor

Hey Community, I wrote a client script for my catalog item, which automatically fills the manager name based on requestor id, so that I created script include but in client script glide ajax when logging response it returns null, could you please help.
I am sharing my code here.


var test_script = Class.create();
test_script.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getName: function() {
        var managerName="";
        var managerID = this.getParameter("sysparm_managerid");
        var usr = new GlideRecord("sys_user");
        usr.addQuery('sys_id', managerID);
        usr.query();
        if(usr.next()) {
            var username= usr.getDisplayValue("user_name");
			managerName=username;
        }
        return managerName;
    },
    type: 'test_script'
});
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.getReference("requested_for", function(ref) {
        if (ref.manager == ""||!ref) {
            g_form.setDisplay("manager_name", false);
        } else {
            var ag = new GlideAjax('test_script');
            ag.addParam("sysparm_name", 'getName');
            ag.addParam("sysparm_managerid", ref.manager);
            ag.getXMLAnswer(function(response) {
				console.log(response);
            g_form.setValue("manager_name",response);
            g_form.setDisplay("manager_name",true);
            });
            
        }
    });
    //Type appropriate comment here, and begin script below

}
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

GlideAjax is an alternative to getReference when you want to do more than a simple lookup, so don't use both. You would want to send the GA parameter of requesrted_for, not the manager, but before we go any further correcting these scripts, have you tried the variable Auto-Populate feature instead of scripts in this case?

BradBowman_0-1754414921805.png

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

GlideAjax is an alternative to getReference when you want to do more than a simple lookup, so don't use both. You would want to send the GA parameter of requesrted_for, not the manager, but before we go any further correcting these scripts, have you tried the variable Auto-Populate feature instead of scripts in this case?

BradBowman_0-1754414921805.png

 

Chaitanya ILCR
Mega Patron
var test_script = Class.create();
test_script.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    // New method to get the manager name based on the requested_for user's sys_id
    getManagerName: function() {
        var managerName = "";
        var requestedForID = this.getParameter("sysparm_requested_for_id");

        if (requestedForID) {
            // First, get the 'requested_for' user record
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(requestedForID)) {
                // Now, check if the user has a manager
                var managerID = userGR.getValue('manager');
                if (managerID) {
                    // Finally, get the manager's record to retrieve their name
                    var managerGR = new GlideRecord('sys_user');
                    if (managerGR.get(managerID)) {
                        managerName = managerGR.getValue('name');
                    }
                }
            }
        }
        return managerName;
    },
    type: 'test_script'
});

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        // Clear the manager name field and hide it if the requested_for field is empty
        g_form.setValue("manager_name", "");
        g_form.setDisplay("manager_name", false);
        return;
    }

    // newValue holds the sys_id of the 'requested_for' user
    var requestedForID = newValue;

    // Use GlideAjax to get the manager's name directly from the server
    var ga = new GlideAjax('test_script');
    ga.addParam("sysparm_name", 'getManagerName');
    ga.addParam("sysparm_requested_for_id", requestedForID);

    ga.getXMLAnswer(function(response) {
        // The 'response' is the manager's name returned from the Script Include
        if (response) {
            console.log("Manager Name received: " + response);
            g_form.setValue("manager_name", response);
            g_form.setDisplay("manager_name", true);
        } else {
            // If no manager is found, clear the field and hide it
            g_form.setValue("manager_name", "");
            g_form.setDisplay("manager_name", false);
        }
    });
}

 Update your scripts @venkatsaladi 

 

Or you can go with what @Brad Bowman  

 

Regy

Chaitanya 

Thanks @Chaitanya ILCR  I will try this step