GROW

code

No getXMLWait() in client scripts

This issue is found automatically by CODA.
Time to fix: 01h 15min

Why is this an issue?

GlideAjax getXMLWait() function tells the user that he must wait for an answer. This causes a block of code continuation until you get a response.

On slow connections, this will bring slow response to the end user. Imagine a person trying to access, from your mobile phone 3G, some page in ServiceNow Portal and a client script prevents it because it is looking for information that does not even suit it.

A example of this, would be:

1
2
3
4
5
var ga = new GlideAjax("HelloWorld");
ga.addParam("sysparm_name", "helloWorld");
ga.addParam("sysparm_user_name", "Bob");
ga.getXMLWait();
alert(ga.getAnswer());

Best practices

Synchronous calls to the server can cause the application to seem unresponsive to the end user. Consequently, the best way to do this communication is using asynchronous calls, such as getXML(), since it does not stop any code process flow.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var comments = gel("dialog_comments").value;
// Call script include to escape text
var ga = new GlideAjax('validateComments');
ga.addParam('sysparm_name', 'validateComments');
ga.addParam('sysparm_comments', comments);
ga.getXML(callback);

return false;

function callback(response) {
  var comments = response.responseXML.documentElement.getAttribute("answer");
  comments = trim(comments);
  if (comments == "") {
     // If comments are empty, alert the user and stop submission
     alert("Please enter your comments before submitting.");
  } else {
    GlideDialogWindow.get().destroy();
    // Set the "Comments" field with comments in the dialog
    g_form.setValue("comments", comments);
  }

The code above shows you an excellent example of how to make a responsive call and in a way that does not interfere with end user navigation and usability.