Alternative to getXMLWait in onLoad Catalog Client script

victor21
Tera Guru

Hi Listers,

I know getXMLWait is no longer supported in Service Portal but could someone suggest an alternative in onLoad Client Script.

Use case: I need to make an Ajax call to populate a Catalog Item variable - I cannot continue with processing until this variable is populated. How do I make the script wait for Ajax call completion?

 

Thanks for any suggestion.

 

Regards,

 

Victor

1 ACCEPTED SOLUTION

victor21
Tera Guru

Hi,

I finally settled for not-so elegant solution after some investigation.

I decided to use setTimeout to delay processing - so, following Ratnakar's example:

function onLoad() {
	var hasReturn = false;
	var variable = g_form.getValue('variable_name');
	var ga = new GlideAjax('AjaxGetVariableValue');
	ga.addParam('sysparm_name', 'getVariableValue');
	ga.addParam('sysparm_variable', variable);
	ga.getXML(function(response) {
		var value = response.responseXML.documentElement.getAttribute('answer');
		// Processing logic here, using the value returned from the Ajax call
		hasReturn = true;
	});
  
  checkAjax(); //delay further processing
  
  function checkAjax() {
	  if (!hasReturn){
			setTimeout(checkAjax, 300);
		}
  }
}

Not so elegant but seems to be working.

 

Regards,

Victor

View solution in original post

4 REPLIES 4

Ratnakar7
Mega Sage
Mega Sage

HI @victor21 ,

 

You can use Promises to handle asynchronous calls in onLoad Client Script.

Here is an example of how you can use Promises to make an Ajax call and wait for its completion before proceeding with processing:

 

 

function onLoad() {
  var variable = g_form.getReference('variable_name');
  var promise = getVariableValue(variable);
  promise.then(function(value) {
    // Processing logic here, using the value returned from the Ajax call
  });
}

function getVariableValue(variable) {
  return new Promise(function(resolve, reject) {
    var ga = new GlideAjax('AjaxGetVariableValue');
    ga.addParam('sysparm_name', 'getVariableValue');
    ga.addParam('sysparm_variable', variable);
    ga.getXML(function(response) {
      var value = response.responseXML.documentElement.getAttribute('value');
      resolve(value);
    });
  });
}

 

 

 

Also, Refer Asynchronous Scripting in ServiceNow  & Callbacks, Promises and Async/Await 

 

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the āœ…Accept solution button and giving it a thumbs up šŸ‘. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

 

Thanks Ratnakar

What I am really looking for is quite the opposite - I need synchronous Ajax call.

 

Regards,

Victor 

Prince Arora
Tera Sage
Tera Sage

@victor21 

 

You can add a loader to the screen, till the time form got the response from backend, loader can be activated and once received destroy the loader.

 

If my answer solved your issue, please mark my answer as āœ…Correct & šŸ‘Helpful based on the Impact.

victor21
Tera Guru

Hi,

I finally settled for not-so elegant solution after some investigation.

I decided to use setTimeout to delay processing - so, following Ratnakar's example:

function onLoad() {
	var hasReturn = false;
	var variable = g_form.getValue('variable_name');
	var ga = new GlideAjax('AjaxGetVariableValue');
	ga.addParam('sysparm_name', 'getVariableValue');
	ga.addParam('sysparm_variable', variable);
	ga.getXML(function(response) {
		var value = response.responseXML.documentElement.getAttribute('answer');
		// Processing logic here, using the value returned from the Ajax call
		hasReturn = true;
	});
  
  checkAjax(); //delay further processing
  
  function checkAjax() {
	  if (!hasReturn){
			setTimeout(checkAjax, 300);
		}
  }
}

Not so elegant but seems to be working.

 

Regards,

Victor