Asynchronous GlideAjax in onSubmit not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2017 09:48 PM
Hi,
I have a single line variable in sp which i need to validate whether this value exists in the table record. if it exists, it will prevent the form from submmiting and shows the error message below the variable.
I am using the following in catalog client script with script include using async glideajax. can anyone help to advise as i cant seem to get the answer to stop my form submission? Thank you!
g_form.hideFieldMsg('name');
var validate = false;
if(g_form.getValue('name') != '')
{
checkName();
}
if(validate == false)
{
g_form.submitted = false;
return false;
}
else {return true;}
}
function checkName(){
var ga = new GlideAjax('nameAjax');
ga.addParam('sysparm_name','checkname');
ga.addParam('sysparm_namelist',g_form.getValue('name'));
validate = ga.getXML(serverResponse);
}
function serverResponse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true')
{
g_form.showFieldMsg('name','name Exists','error');
validate = false;
return false;
}
else{
validate = true;
return true;}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2017 09:54 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
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2017 10:27 PM
Like all things that are Best Practice, there are always Use Cases where they don't apply.
This is one of them.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2017 09:56 PM
Client Callable is checked in the script include ? Also please share script include code ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-22-2017 10:03 PM
Hello Wayner,
Use log statements for checking whether values which are passing are correct or wrong and also you need to use synchronous glideajax.
Below is the modified script:
g_form.hideFieldMsg('name');
var validate = false;
var Name = g_form.getValue('name'); // check whether name is having thecorrect value or not.
alert(Name);
if(Name != ' ')
{
checkName();
}
else{
validate = true;
return validate;
}
function checkName(){
var ga = new GlideAjax('nameAjax');
ga.addParam('sysparm_name','checkname');
ga.addParam('sysparm_namelist',Name);
ga.getXMLWait();
alert(ga.getAnswer());
var answer = ga.getAnswer()
if(answer == 'true')
{
g_form.showFieldMsg('name','name Exists','error');
validate = false;
return validate;
}
else{
validate = true;
return validate;
}
}
}
try and let me know.