onsubmit client script help - submitting when it shouldn't
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 10:14 AM
I have this script and although i get the alert/infomessage it is still submitting
function onSubmit() {
var dateTimeString = g_form.getValue('date');
// console.log("Check count of Requests - dateTimeString = " + dateTimeString);
var dateOnly = dateTimeString.split(' ')[0];
//console.log("Check count of Requests - dateOnly = " + dateOnly);
var ga = new GlideAjax('CountCheck');
ga.addParam('sysparm_name', 'getCount');
ga.addParam('sysparm_date', dateOnly);
ga.getXML(reply);
function reply(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("Check count of Requests - answer = " + answer);
console.log("Check count of Requests - dateOnly = " + dateOnly);
if ((dateOnly == '24/08/2023' || dateOnly == '25/08/2023' || dateOnly == '26/08/2023' || dateOnly == '27/08/2023' || dateOnly == '28/08/2023') && answer < 2) {
alert("Date available and your request has been submitted for" + dateOnly + ". The process will action on the chosen date, we will endevaour to email you a reminder prior to the changes being implemented.");
return true;
}
else if ((dateOnly != '24/08/2023' || dateOnly != '25/08/2023' || dateOnly != '26/08/2023' || dateOnly != '27/08/2023' || dateOnly != '28/08/2023') && answer >= 2) {
return false;
g_form.addInfoMessage("Date "+ dateOnly + " not available. Select a different date.");
alert("Date "+ dateOnly + " not available. Select a different date.");
}
else {
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 10:28 AM
Hello @dave_edgar ,
Try getXMLWait() instead of getXML().
Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 10:29 AM
@dave_edgar Since you are making an asynchronous GlideAjax call, hence the form is getting submitted despite the alert being shown.
Please refer to this support article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579 where the script initially returns false to stop the form submission and returns true/false once the GlideAjax response is received from the server.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2023 11:11 AM - edited 08-23-2023 07:35 AM
Hey @dave_edgar
The form is still submitted due to the Asynchronous GlideAjax call. The form is submitted before the response arrives, causing your validations to not execute.
To address this, your script should look like the example below.
function onSubmit() {
var dateTimeString = g_form.getValue('date');
var dateOnly = dateTimeString.split(' ')[0];
/*** Pop this gem into your script! */
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName();
var ga = new GlideAjax('CountCheck');
ga.addParam('sysparm_name', 'getCount');
ga.addParam('sysparm_date', dateOnly);
ga.getXML(reply);
function reply(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if ((dateOnly == '24/08/2023' || dateOnly == '25/08/2023' || dateOnly == '26/08/2023' || dateOnly == '27/08/2023' || dateOnly == '28/08/2023') && answer < 2) {
alert("Date available and your request has been submitted for" + dateOnly + ". The process will action on the chosen date, we will endevaour to email you a reminder prior to the changes being implemented.");
/*** Pop this gem into your script! */
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
} else if ((dateOnly != '24/08/2023' || dateOnly != '25/08/2023' || dateOnly != '26/08/2023' || dateOnly != '27/08/2023' || dateOnly != '28/08/2023') && answer >= 2) {
g_form.addInfoMessage("Date " + dateOnly + " not available. Select a different date.");
alert("Date " + dateOnly + " not available. Select a different date.");
return false;
}
return false;
}
return false;
}
Check out this KB reference: #KB0779964
Cheers
Tai Vu
Feel free to give that thumbs-up button a little tap dance if the response deserves a gold star! 🌟