- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-09-2023 02:28 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-10-2023 11:47 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-09-2023 02:49 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-09-2023 11:55 AM
Thanks Ratnakar
What I am really looking for is quite the opposite - I need synchronous Ajax call.
Regards,
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-09-2023 02:49 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā05-10-2023 11:47 PM
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