Onsubmit is not preventing submission
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2023 11:55 PM
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;
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2023 12:08 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2023 12:12 AM
@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.
Regards, Shekhar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2023 12:24 AM
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.