How to abort Onsubmit Client script with condition

shraddha pangan
Tera Contributor

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! 

3 REPLIES 3

Kieran Anson
Kilo Patron

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

Sai_Charan_K
Kilo Sage

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

sadif_raja
Tera Guru

@shraddha pangan 

 

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:

  1. Make your GlideAjax call.
  2. 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:

 

javascript:
function onSubmit() { // Define a variable to control submission var submitFlag = false; var ga = new GlideAjax('YourScriptIncludeName'); // replace with your script include name ga.addParam('sysparm_name', 'yourFunction'); // replace with your function name in the script include ga.addParam('sysparm_conditionA', 'value'); // send any necessary parameters // Perform the GlideAjax call ga.getXMLAnswer(function(response) { // Response returned from the server-side function var answer = response; if (answer == 'true') { // Condition A is true, prevent form submission submitFlag = false; g_form.addErrorMessage("Condition A is true. Form cannot be submitted."); } else { // Condition A is false, allow form submission submitFlag = true; } }); // Ensure form submission is blocked until the response is received return submitFlag; }

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!