onSubmit client script with asynchronous GlideAjax postal-code validation does not resubmit the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
an hour ago
Here’s a detailed ServiceNow Community post you can use. Fill in the bracketed details before posting.
Title:
onSubmit client script with asynchronous GlideAjax postal-code validation does not resubmit the form
Post:
I’m trying to validate a postal code before allowing a ServiceNow form/catalog request to submit. The client script calls a Script Include using GlideAjax, holds the initial submission by returning false, and then attempts to resubmit after the callback returns.
The flow is:
- Read the country and postal-code fields.
- Skip validation if either field is blank or if the exact values were already validated.
- Set a validation flag to IN_PROGRESS while the GlideAjax call runs.
- If the server response is valid, store a key for the validated country/postal-code pair and call gsf...
- If invalid, show a field-level error and keep the form from submitting.
Issue:
The script is not working as expected. Specifically, [describe what happens: the form submits without validation / remains stuck / validation runs but does not resubmit / an error appears]. The browser console shows [paste the exact error, if any].
Expected behavior:
The form should remain unsubmitted while validation is running, display an error if the postal code is invalid, and submit the original action (such as Order Now or Add to Cart) once validation succeeds.
Environment:
Could someone advise whether this asynchronous onSubmit and resubmission pattern is supported in this interface, and what the recommended approach is? I’m particularly unsure whether gsftSubmit and g_form.getActionName() are appropriate for the interface and form type I’m using.
Troubleshooting details to include if available:
Please also note whether the GlideAjax callback runs, what answer contains, and whether postal_validated is an actual field/variable on the form. Those details can help distinguish a server-response issue from a resubmission or client-script compatibility issue.
function onSubmit() {
var COUNTRY_FIELD = 'country1';
var POSTAL_FIELD = 'postal_code1';
var FLAG_FIELD = 'postal_validated';
var country = g_form.getValue(COUNTRY_FIELD);
var postalCode = g_form.getValue(POSTAL_FIELD);
if (!country || !postalCode) {
return true;
}
// Already validated for exactly these values -> let the submit through
var currentKey = country + '|' + postalCode;
if (g_form.getValue(FLAG_FIELD) === currentKey) {
return true;
}
// A validation call is already running -> keep the form held
if (g_form.getValue(FLAG_FIELD) === 'IN_PROGRESS') {
return false;
}
g_form.setValue(FLAG_FIELD, 'IN_PROGRESS');
// Capture the clicked button (Order Now / Add to Cart) before the async call
var actionName = g_form.getActionName();
var ga = new GlideAjax('PostalCodeValidator');
ga.addParam('sysparm_name', 'validatePostalCode');
ga.addParam('sysparm_country', country);
ga.addParam('sysparm_postal_code', postalCode);
ga.getXMLAnswer(function(answer) {
try {
var result = JSON.parse(answer);
g_form.hideFieldMsg(POSTAL_FIELD, true);
if (!result.valid) {
g_form.setValue(FLAG_FIELD, '');
g_form.showFieldMsg(POSTAL_FIELD, result.message, 'error');
return;
}
// Store the validated values, then re-submit
g_form.setValue(FLAG_FIELD, currentKey);
gsftSubmit(null, g_form.getFormElement(), actionName);
} catch (e) {
console.log(answer);
console.log(e);
g_form.setValue(FLAG_FIELD, '');
g_form.showFieldMsg(POSTAL_FIELD, 'Unable to validate postal code.', 'error');
}
});
return false; // hold the submit until the server answers
}