glideajax in servicenow

1876

GlideAjax is a powerful client-side API in ServiceNow that allows developers to make asynchronous calls to the server without reloading the page. GlideAjax can be used to retrieve data from the server, update server-side data, and perform other operations.

To use GlideAjax, you need to create a server-side script that performs the desired operation and returns the data.    

glideAjax in details-https://servicenowwithrunjay.com

Types Of GlideAjax

1. Synchronous GlideAjax:

Synchronous means whenever you make a request to fetch values from server in your client script, control will wait there until it doesn’t get a response from server. getXMLWait() method used to make Synchronous call.

 2. Asynchronous GlideAjax:

Async mean regardless of request next line of code gets executed. It does not wait for response. getXML() method used to make Asynchronous call.

 

Now let’s understand Asynchronous GlideAjax call in details.

As I have explained above that use of GlideAjax is to get data from server side, it means there are two part of script we have to write.

      1. Client-side script- To call server-side script using GlideAjax
      2. Server-side script- To execute the logic and interact with database at server-side.

Now let’s understand the client-side script in detail. Here you need to understand each component of code and have to follow the same during your implementation.

Requirement

We will understand this with real world example. Suppose we got a requirement from client stating While creating incident if user select VIP caller, then set the priority critical.

 

Solution

From this requirement it is very much clear that we need to check that selected user in caller field is VIP user or not. VIP attribute details are not present at client side so it is clear that we have to write server-side script to get that value. Now let’s explore the solution.

      1. We can use create display business rule and get the VIP value using g_scratchpad variable at client side, there is no issue on this, we can fulfil the requirement but the question here, is it a best solution? the answer in no. It will execute in each time when form loads which may cause performance issue.
      2. In 2nd approach we will use GlideAjax and script include to get the VIP value from server. Here we will be creating On-change client script and calling script include method. In this approach we will be making server call when ever required. In our example will make server call when user change the caller field value.

Now I am going to explain each component which are used in make server call from client side. Syntax is important here, so you have to follow the same which I have explained in below pic.What

Client Script: Create new onChange client script and use below code.

glideAjax in details-https://servicenowwithrunjay.com

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below

    var ga = new GlideAjax('UserUtils');
    ga.addParam('sysparm_name', 'checkVipUser');
    ga.addParam('sysparm_caller_sys_id', newValue);
    ga.getXML(ValidateVipUser);

    function ValidateVipUser(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        
        if (answer=='true') {
            g_form.setValue('priority', 1);
            g_form.setValue('urgency', 1);
            g_form.setValue('impact', 1);
        } else {
            g_form.clearValue('priority');
            g_form.clearValue('urgency');
            g_form.clearValue('impact');
        }
    }

}

 

Script include: Create new script include and use below code.

var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkVipUser: function() {
        var caller = this.getParameter('sysparm_caller_sys_id');
        var gr = new GlideRecord('sys_user');
        gr.get(caller);

        return gr.vip;

    },

    type: 'UserUtils'
});

 

How to Return multiple values to GlideAjax from Script include

FAQ

What is glideajax in servicenow?

GlideAjax is a powerful client-side API in ServiceNow that allows developers to make asynchronous calls to the server without reloading the page. GlideAjax can be used to retrieve data from the server, update server-side data, and perform other operations.

What is Asynchronous GlideAjax?

Async mean regardless of request next line of code gets executed. It does not wait for response. getXML() method used to make Asynchronous call.

What is Synchronous GlideAjax?

Synchronous means whenever you make a request to fetch values from server in your client script, control will wait there until it doesn’t get a response from server. getXMLWait() method used to make Synchronous call.

LEAVE A REPLY

Please enter your comment!
Please enter your name here