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 10:20 PM
Please check if this helps.
g_form.hideFieldMsg('name');
var validate = true;
if(g_form.getValue('name') != ''){
checkName();}
if(validate == false){
g_form.setValue('u_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'));
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;}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2017 01:24 AM
Hi Shishir,
This looks like asynchronous ajax. can i check is asynchronous can be used also?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2017 02:10 AM
Hi Wayner,
we can have 2 approches for this to achieve synchronously.
1. add a check box variable on form, put a check that this button needs to be checked before submitting the form. and put synchronous ajax onchange of the checkbox.
2. add ui macro to form, which will have synchrnous ajax call to verify information.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-23-2017 05:40 PM
It's a lot of effort for something that can be done in 18 lines of code in one client script.
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
‎10-13-2017 06:45 AM
Thanks alot everyone for your suggestion.
I used onchange with showfieldmsg to inform the requestor that record he is submitting exists.