You are currently viewing ServiceNow GlideAjax: An Easy Decoding 2024

ServiceNow GlideAjax: An Easy Decoding 2024

Websites are enumerating many uses of using Ajax on web pages. On similar grounds, ServiceNow offers GlideAjax class & its functions to access Ajax capabilities within the ServiceNow ecosystem. In this article, I am decoding GlideAjax very simply to make it easy to understand & use.

What GlideAjax class do?

The GlideAjax class enables a client script to call server-side code in a script include.

Steps to use ServiceNow GlideAjax on the Client Side?

  • Create a GlideAjax instance by calling the GlideAjax constructor. As the argument to the constructor, specify the script’s name including the class that contains the method you want to call.
  • Call the addParam method with the sysparm_name parameter and the name of the script-include method you want to call.
  • (Optional) Call the addParam method one or more times to provide the script include code with other parameters it needs.
  • Execute the server-side code by calling getXML() or getXMLWait()

Difference Between getXML() & getXMLWait()

ServiceNow GlidAjax Example Using getXML()

In this example, we are getting the logged-in User manager’s information.

We have written an OnSubmit() client script (Test – GlideAjax) on incident form, which is calling a script include (GetUsersDetails) and method getManager

// Client Script Code

function onSubmit() {
    var ga = new GlideAjax('GetUsersDetails'); // GetUsersDetails is the script include class
    ga.addParam('sysparm_name', 'getManager'); // getManager is the script include method
    ga.addParam('sysparm_user_name', g_form.getValue('caller_id')); // Set parameter sysparm_user_name to caller
    ga.getXML(GetDetails);
   
	
    // the callback function for returning the result from the server-side code
    function GetDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
    }

}
// Script Include Code

var GetUsersDetails = Class.create();
GetUsersDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManager: function() {
        var userName = this.getParameter("sysparm_user_name");
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', userName);
        grUser.query();
        if (grUser.next())
            return grUser.manager.getDisplayValue();
    },
    type: 'GetUsersDetails'
});

GlidAjax Example Using getXMLWait()

Here we are demonstrating the same information but using the getXMLWait() function.

// Client Script code

function onSubmit() {
    var ga = new GlideAjax('GetUsersDetails'); // GetUsersDetails is the script include class
    ga.addParam('sysparm_name', 'getManager'); // getManager is the script include method
    ga.addParam('sysparm_user_name', g_form.getValue('caller_id')); // Set parameter sysparm_user_name to caller
    ga.getXMLWait();
    alert(ga.getAnswer()); // No need for a callback function
}
// Script Include Code

var GetUsersDetails = Class.create();
GetUsersDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManager: function() {
        var userName = this.getParameter("sysparm_user_name");
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', userName);
        grUser.query();
        if (grUser.next())
            return grUser.manager.getDisplayValue();
    },
    type: 'GetUsersDetails'
});

I hope these examples will help you in learning GlideAjax easily and intuitively.

Leave a Reply