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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2015 01:52 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2015 02:21 PM
Indeed it runs after the database commits so, if you are trying to validate something before database commit, It will not give a return/desired return value in my opinion.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2015 04:04 PM
Since the example script you provided is just a generic example from the wiki it's hard to tell what your script actually needs. Are you just needing to get a value from the server side? Or do you need to pass in values to the script include and make calculations to get the value?
If you just need to get a value from the server side, try using g_scratchpad together with a client script. g_scratchpad is a "Display" business rule used to pass server-side values to client scripts.
Scripting in Business Rules - ServiceNow Wiki

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2015 04:58 PM
The script is a catalog client script so i believe using scratch pad is not an option.
the client script essentially passes user selected input to a script include which queries a table with this input and based on the data returns true or false value. if the value is false then the validation fails and the catalog shouldn't be allowed to submit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2015 05:06 PM
You will have to use a synchronous Ajax call because the response will probably come back after the client script finishes running and you've moved on from the form. The onSubmit Client Script is one of a very few scenarios where you do not want to run an asynchronous Ajax call.
The synchronous call will wait for the response before continuing. It might be a little counter-intuitive, but you are validating data and the user is not really waiting to do anything else, just for the submit to finish. Asynchronous calls and onSubmit scripts are just not compatible.