How to use asynchronous GlideAjax in on submit client script for validation?

Ravish Shetty
Tera Guru

Hello all,

We have a on submit client script which uses synchronous GlideAjax. As per ServiceNow's recommendations, we are making it asynchronous. The concern is that as below, we are waiting at line 4 for a response from the server and based on it we are either allowing the form to submit or not.

If we replace the synchronous logic with asynchronous, there will not be any waiting at line 4 and the form will be submitted without the validation taking place.

var ga = new GlideAjax('HelloWorld');

ga.addParam('sysparm_name','helloWorld');

ga.addParam('sysparm_user_name',"Bob");

ga.getXMLWait();

if(ga.getAnswer() != ''){

return true;

} else {

return false;

}

Edit: This is a catalog client script

10 REPLIES 10

It is important to understand why a particular solution is 'Best Practice' before jumping in, as there will always be a scenario where best practice is not the best solution and I would say that this is one of those scenarios.



Asynchronous GlideAjax is best practice because


  • Performance
    • It is more efficient to return only the data you need from the Server, rather than the entire GlideRecord which returns all data of the entire record
  • Usability
    • GlideAjax is Asynchronous, so the UI does not freeze up while waiting for a response (As Jim has mentioned)


Alas, there is no reason why you shouldn't use gs.getXMLWait(); as the usability point is in conflict with what you are trying to achieve.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Excellent information on GlideAjax here as well, complete overview, and echoes some of your points: ServiceNow Pro Tips — GlideRecord & GlideAjax: Client-Side Vs. Server-Side


vevgpro
Kilo Contributor

The link is unfortunately dead.. Any other places to get this article?


As Paul (et al) stated, the recommendation for using async GlideAjax is based on performance because the client does not freeze waiting for a response.   However this may not suit every circumstance.



In your case you NEED to wait while it processes your AJAX request, so async does not fit as a solution.



==> use synchronus AJAX... getXMLwait()




-Brian