The CreatorCon Call for Content is officially open! Get started here.

change GlideAjax method from GetXML() to GetXMLWait()

sureshp89882164
Tera Contributor
Scenario is: A 'Pop-Up' message will appear on-screen when any end-user attempts to re-assign (more than one time) already assigned incident to the Technical Service Desk (TSD).
 
Which one is the best one either using with GetXML() OR GetXMLWait()
Currently using with GlideAjax method GetXML(), what changes i have to do if I change from GetXML() to GetXMLWait() on below code:
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
 var ga = new GlideAjax('assignmentCountincident');
    ga.addParam('sysparm_name', 'getIncidentCount');
    ga.addParam('sysparm_inc_sysid', g_form.getUniqueValue());
    ga.getXML(responseParse);

 

function responseParse(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if ((answer>=1) && (newValue == 'f1bb47cadb2e83008449f6e9bf96190f')){
       
alert('Incidents are not to be reassigned back to TSD for resolution, customer call back and/or further team re-assignment. );

 

}
}
}
 
script include: assignmentCountincident
 
6 REPLIES 6

debendudas
Mega Sage
Mega Sage

Hi @sureshp89882164 ,

For a client-side onChange script that displays an alert, using getXMLWait() is generally not the recommended best practice. The current getXML() approach, which is asynchronous, is better for user experience. 

 

getXML() vs. getXMLWait() for your scenario:

 

1. getXML() (Asynchronous)

  • How it works: The script sends a request to the server and continues processing without waiting for the response. When the response is received, the callback function (responseParse in your code) is executed.
  • Pros: The UI remains responsive. The user can continue to interact with the form while the server-side lookup is happening.
  • Cons: Since the script doesn't pause, the alert will pop up after the user has already changed the assignment group. If the user clicks "Save" very quickly, the alert might appear too late to prevent the re-assignment. 

2. getXMLWait() (Synchronous)

  • How it works: The script pauses and freezes the user interface until a response is received from the server.
  • Pros: You are guaranteed to get the server's response before the rest of the client-side code continues. This means the alert will fire immediately after the value change, and you could add logic to prevent form submission.
  • Cons: This is considered bad practice because it freezes the UI, creating a poor user experience, especially on slow connections. It is generally discouraged by ServiceNow. 

 

If this solution helps you then, mark it as accepted solution ‌‌✔️ and give thumbs up 👍

Here is the SN documentation you can check out for the code: https://www.servicenow.com/docs/csh?topicname=c_GlideAjaxAPI.html&version=latest

Connectmustaq
Tera Contributor

Hi @sureshp89882164 ,

 

To change from getXML() to getXMLWait() in your ServiceNow client script, the main difference is how the callback is handled and how the processing is performed. Below is a direct comparison and update to your code as required for using getXMLWait().


Switching from getXML() to getXMLWait() makes the GlideAjax call synchronous, meaning the browser waits for the response before continuing. Using getXML() (asynchronous) is generally recommended to avoid UI freezing, but if synchronous waiting is desired, getXMLWait() can be used. The callback structure must be adjusted accordingly.

Done modification of code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('assignmentCountincident');
ga.addParam('sysparm_name', 'getIncidentCount');
ga.addParam('sysparm_inc_sysid', g_form.getUniqueValue());
// getXMLWait returns the XML synchronously:
var response = ga.getXMLWait();
// Directly access the answer attribute from the returned XML
var answer = response.documentElement.getAttribute("answer");
if ((answer >= 1) && (newValue == 'f1bb47cadb2e83008449f6e9bf96190f')) {
alert('Incidents are not to be reassigned back to TSD for resolution, customer call back and/or further team re-assignment.');
}
}

Keep using getXML() for non-blocking, responsive UI unless synchrony is strictly needed. If switching, use the code above and be aware of the impact on user experience.

 

Please appreciate my efforts, help and support extended to you by clicking on – “Accept as Solution”; button under my answer. It will motivate me to help others as well.

 

Thanks & Regards,

Mohammed Mustaq Shaik

kaushal_snow
Mega Sage

Hi @sureshp89882164 ,

 

To modify your GlideAjax implementation from getXML() to getXMLWait(), it's important to note that getXMLWait() is deprecated and no longer supported in ServiceNow, particularly within Service Portal environments. The recommended approach is to continue using getXML() and handle the server response asynchronously through callback functions, ensuring that your client script remains non blocking and responsive....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/