Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

GlideAjax call multiple functions

pavel_martinec
Tera Contributor

Hello,

I would like to ask for hint of solution over GlideAjax.

I have UI Action, which works completely as Client side button and calls GlideAjax.

In this Ajax script include is a very time consuming function, from fhich I would like to push some info on states.

Sadly, Ajax is only one-time request-response, so I need to ask for state in some intervals.

 

The idea is this, but I am pretty sure this will not work, because in this solution, I will rewrite GlideAjax object and I belive, the first response will be lost, same as any other in the interval:

 

UI Action:
==========
var syncGA = new GlideAjax("Sync_ScriptInclude_Ajax");
syncGA.addParam('sysparm_name', 'timeConsumingOperation');
syncGA.addParam('sysparm_parm1', "...");
syncGA.getXML(parseDoneResponse);
 
function parseDoneResponse(response) {
  //operation was done, say Done to user
  clearInterval(check);
}

var check = setInterval(function(){ 
syncGA.addParam('sysparm_name', 'getState');
syncGA.getXMLWait(parseState);
}, 3000);


function parseState(response) {
  //update state message, percentage, whatever
}


Script Include
==============

var Sync_ScriptInclude_Ajax = Class.create();
Sync_ScriptInclude_Ajax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
initialize:function(){
this.result = [];
},

timeConsumingOperation:function(){
//some logic, lets say GlideRecord here with result
while(step-by-step){
this.result.push(step-result);
}
},

getState:function(){
return JSON.stringify(this.result);
},

	type: 'Sync_ScriptInclude_Ajax'
});

Is this somehow possible? Or s there any other fancy way, how to do this?

 

Thanks in advance 🙂

6 REPLIES 6

sebastian_g_snc
ServiceNow Employee

Hi Pavel,

I have understood that you would like to invoke the AJAX call every 3 seconds repeatedly. Is that correct?
Why are you using the getXMLWait function there? I am sure you know, that this is the synchronous call.

Did you try to figure out, how the built-in auto-refresh works? that might point you into the right direction.

Another idea - which will only work for a limited amount of stated/messages of course - is to get the information in the first round-trip and store them in the g_scratchpad on client side.

Kind regards

Seb

The problem is, that I need to call 2 functions, that are in one instance of GlideAjax.

That XMLWait is there intentionally as I don't want to tick the interval untill I get the response.

The core of the issue is, If there exists a way, how to call 2 spearate functions in same script inclide instance within one GlideAjax definition.

 

You could call another function from one function in script include itself.

 

this.getState();

I know, but I need to push results. I will maybe draw an image:
find_real_file.png