AJAX again

Hello Friends

Today I am writing again about AJAX – I actually wrote back in 2014 a blog post about this very same topic but it seems that I really like this topic so I thought to write about it again.

before getting into the weeds, quick recap of two concepts

Synchronous is real-time communication method where each party receives messages instantly and requestor needs to wait for the request to be complete.

Asynchronous  is a communication method of exchanging messages between parties in which each party receives and processes messages whenever it’s possible to do so, rather than doing so immediately upon receipt. requestor immediately continues with its work without caring for the immediate result.

When to use GlideAjax ?

We should use GlideAjax when we want trigger a server function from the client side

Example

Let’s say that you as Service desk manager, want your customer to see the last opened incident for them, this should happen when they try to report an outage.

from technical prospective, how we can do that? we need to

  1. Get User ID
  2. Query Incident table where the caller is equals to User ID
  3. print out incident number

so i listed out the above steps, if you think about this for a moment, the only way to achive this is via server call from client side hence AJAX comes into play !

Let’s do this together:

Script include

First step we need to create is client callable script include which extends AbstractAjaxProcessor

Second step, we need to create a function, this function should get the last incident created for the user that wants report an outage

var AJAXLastINC = Class.create();
 AJAXLastINC.prototype = Object.extendsObject(AbstractAjaxProcessor, {
     getLastINC: function() {
         var vl = this.getParamter('sysparm_userid');
         var gr = new GlideRecord("incident");
         gr.addQuery("caller_id.sys_id", vl);
         gr.query();
         gr.orderByDesc('opened_at');
         if (gr.next()) {
             return 'Your last open Incident number ' + gr.number + ' ';
         }
 }, type: 'AJAXLastINC'
 });

Client script

Third steps, Create client script to call your script include. Create a GlideAjax instance, specify the name of the script include as argument to the constructor class that contains the method you want to call.

Use addParam method with the sysparm_name parameter and the name of the script-include method you want to call.

Use addParam method with the sysparm_userid parameter to provide caller ID to the script include

Use getXML() to execute server code

function onLoad() {
 //Type appropriate comment here, and begin script below
     var ga = new GlideAjax('AJAXLastINC'); // HelloWorld is the script include class 
 ga.addParam('sysparm_name','getLastINC'); // helloWorld is the script include method 
 ga.addParam('sysparm_userid',g_user.userID); // Set parameter sysparm_user_name to 'Bob' 
 ga.getXML(GetUserINC); 
 }
 function GetUserINC(response) {  
    var answer = response.responseXML.documentElement.getAttribute("answer"); 
     alert(answer);
 }

References and further reading

https://developer.servicenow.com/dev.do#!/reference/api/paris/client/c_GlideAjaxAPI#r_GLAX-GlideAjax_S

https://docs.servicenow.com/bundle/paris-application-development/page/script/ajax/topic/p_AJAX.html

https://developer.mozilla.org/en-US/docs/Glossary/Asynchronous

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Synchronous_request

Much Love.Peace.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s