Prevent onchange script from running when onsubmit script runs

chetan17421
Tera Guru

Hi All,

 

I have written a onchange client script and a onsubmit client script. When onsubmit script is running it is triggering the onchange script. But during onsubmit I dont want to run the onchange script. How can I prevent onchange script from running when onsubmit script runs?

 

 

1 REPLY 1

Ehab Pilloor
Mega Sage

Hi @chetan17421,

In ServiceNow, the behavior you're describing is expected, as the `onSubmit` client script triggers field changes, which, in turn, can invoke the `onChange` client script. To prevent the `onChange` script from running during the `onSubmit` process, you can introduce a mechanism to check and skip the `onChange` logic when it's invoked during the `onSubmit`. Here's how you can approach this:

1. Check for onSubmit flag in onChange script:
- In your `onChange` client script, introduce a check to see if the script is running as part of the `onSubmit` process. You can use a flag or a condition related to the `onSubmit` event.


// onChange client script
function onChange() {
if (!g_form.submitted) {
// Your existing onChange logic here
// ...
}
}

In this example, `g_form.submitted` is a flag that indicates whether the form is in the process of being submitted. If it's `true`, it means the `onSubmit` script is running, and the `onChange` logic is skipped.

2. Set the onSubmit flag in onSubmit script:
- In your `onSubmit` client script, set the flag to indicate that the form is being submitted.

 


// onSubmit client script
function onSubmit() {
g_form.submitted = true;

// Your existing onSubmit logic here
// ...
}

By setting `g_form.submitted` to `true`, you're signaling to the `onChange` script that it should skip its logic during the `onSubmit` process.

This way, you can control the execution of the `onChange` logic based on whether the form is currently being submitted. Always test your scripts thoroughly to ensure they behave as expected in different scenarios.

 

If you found this reply useful, please mark it as solution/helpful.

 

Thanks and Regards,

Ehab Pilloor