Onsubmit is not preventing submission

abed123
Tera Contributor

Hi All,

We have onsubmit client script that needs to prevent the form based on loggedin users company and based on selected variable, it is displaying the field messages and alert but it is submitting the form. please find the below code and please provide suggestions.

function onSubmit() {
//Type appropriate comment here, and begin script below

var hardware = g_form.getValue('variaable_name');
var add = ' ';
var ga = new GlideAjax('getCompany');
ga.addParam('sysparm_name', "checkRecordPresent");
ga.addParam('sysparm_userID', g_form.getValue('requester'));
ga.getXMLAnswer(function(answer){

if(answer != 'not found'){

var parser = JSON.parse(answer);

if(parser.company == 'company sys_id'){
add = 'true';
}

if(add == 'true' && hardware == 'value') {
alert('only for ADDC');
g_form.addInfoMessage('alert message');
g_form.showFieldMsg('variable_name', 'message');

}


}
return false;
});
}

 

3 REPLIES 3

Vaibhav127
Tera Guru

Hi abed123,

GlideAjax is an asynchronous call, so there is no exact order of the code execution. This may be the reason why the submission is failing.

You can try to use abortaction in BR.

Shekhar Navhak1
Kilo Sage
Kilo Sage

@abed123 ,

 

You need to use getXMLWait() since you need to wait until the result comes from the server side.

 

Note:  getXMLWait() is not recommended as it will halt the process until the result comes from the server side.

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @abed123 ,

The GlideAjax (Asynchronous) does not work on onSubmit Client Script. This is because of the fundamental behavior of Asynchronous scripts which are non-blocking by nature.

 

Refer below KB article for Async call in onSubmit client script:-

How to do async validation in an onsubmit client script. 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy