Onsubmit script not works
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi team
I have used client and script include.Client is in " Asset Management Common". script include in Global scope
and client callable and applies on all scope
based on some condition i don't want smbit the form and alert msg or error msg. i tired many ways not works. getting getting java console error while submitting . can you please help me on that
script inculde
client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @sivananda80
Share the error message or error screen shot.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@sivananda80 Please refer to this article https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-... to know the steps to call GlideAjax in an onSubmit client script.
Also, the following piece of code might be causing an issue in your client script.
// Trigger submit again
$('.btn-primary:contains("Submit")').click();
In order to submit the form automatically I recommend using g_form.submit('action name'); Please refer to the article shared above for the more information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I am concerned about this line in your client script:
It is outside of the onSubmit function and also not part of any other function that gets called from onSubmit.
@sivananda80 wrote:
client scriptvar validated = false;function onSubmit() {if (validated) {return true;}...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @sivananda80
The issue is that onSubmit() is asynchronous, but ServiceNow expects it to return true or false immediately. By the time your GlideAjax.getXMLAnswer() callback executes, the original submit has already been cancelled. Triggering $('.btn-primary').click() from inside the callback often causes JavaScript errors or recursive submits, especially in Workspace or newer UI.
Solution (Recommended)
Keep return false; in onSubmit().
In the callback, instead of clicking the button, resubmit the form using ServiceNow APIs:
validated = true; g_form.submit(); // or gsftSubmit(null, g_form.getFormElement(), g_form.getActionName()) in classic UI
If validation fails:
g_form.addErrorMessage(result.message); return;
If g_form.submit() still doesn't work
It usually means the form is in Workspace, where asynchronous validation in onSubmit is not supported reliably.
In that case, the best approach is to:
Perform the GlideAjax validation before the user submits (e.g., onChange or when the Submit UI Action is clicked), or
Move the validation to a Before Business Rule/Script Include invoked server-side, abort the action with:
current.setAbortAction(true); gs.addErrorMessage('Territory is not Opt-In...');This is the most reliable approach because it cannot be bypassed.
In short: Don't use $('.btn-primary').click() to resubmit. Use g_form.submit()/gsftSubmit() if you're in Classic UI, or move the validation server-side if you're in Workspace.