How to abort Onsubmit Client script with condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 03:43 AM
Hi
I have requirement to abort submission if Condition A is True. I have used the GlideAjax in OnSubmit Client script.
I am using "return false" if condition A is true but form gets submitted always.
Thanks in Advance!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 06:39 AM
Hi,
GlideAjax is asynchronous, so the form will submit before a response is received from the ajax call.
Can you share your existing script, and what you're validating. We might be able to offer an alternative approach
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 06:58 AM
Hi @shraddha pangan,
As mentioned by @Kieran Anson, GlideAjax is an asynchronous function, meaning that the form will submit without waiting for the GlideAjax response. To avoid potential issues with asynchronous behavior and streamline your workflow, you might want to consider using an Onchange client script instead. This can help ensure that the necessary checks or actions are performed before the form is submitted, providing a more controlled and seamless user experience.
Please mark my answer "Helpful" and "correct" if you feel that it has helped you in any way.
Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2024 07:09 AM
In ServiceNow, when using GlideAjax within an onSubmit client script, it's important to remember that GlideAjax is asynchronous. This means that the script doesn't wait for the server-side response before continuing execution, which is why your return false may not work as expected.
To handle this, you need to use the callback function of the GlideAjax call. Here's an example approach to handle this scenario correctly:
Steps to abort form submission based on Condition A:
- Make your GlideAjax call.
- Handle the server response in the callback and decide whether to return false or true to prevent/allow submission.
Here's an updated version of your onSubmit client script:
Important notes:
- The getXMLAnswer function handles the asynchronous nature of the GlideAjax call.
- The form will only submit based on the submitFlag, which is set within the callback.
- Ensure the Script Include is properly configured and client-callable to return the required condition.
This should resolve your issue with form submission being allowed even when return false is expected. Let me know if you need more guidance!