Asynchronous GlideAjax in onSubmit not working

wayneryeo
Kilo Contributor

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;}

14 REPLIES 14

Shishir Srivast
Mega Sage

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;}


}



Hi Shishir,



This looks like asynchronous ajax. can i check is asynchronous can be used also?



Thanks.


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.


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

wayneryeo
Kilo Contributor

Thanks alot everyone for your suggestion.



I used onchange with showfieldmsg to inform the requestor that record he is submitting exists.