Preventing Form Submission Before Page Load

We’ve all seen slow loading forms before, ones where it can take a few seconds to finish setting the mandatory fields, the read only fields etc. The issue with this however, is that all the UI actions are actually loaded before the client scripts. Why is this an issue? Well let’s say you had to put in work notes before moving to the next state of a record. The client script could be setting the work notes field as mandatory so the UI action doesn’t actually need to check that it’s been populated, because ServiceNow enforces mandatory fields to be populated. All good and normal so far. But on slow loading forms, the UI action is displayed but the work notes field hasn’t yet been made mandatory, so if the user clicks the action before the form is finished loading, they’re able to perform the action without entering the mandatory fields.

To overcome this, you can implement a global UI script with the following code:

var gsftSubmitHolder = gsftSubmit;
gsftSubmit = function() {
alert('Page is still loading');
};
addAfterPageLoadedEvent( function() {
gsftSubmit = gsftSubmitHolder;
});

So what’s this doing? If you open your browser’s development tools and inspect your UI actions that are not client side UI actions, you’ll see that they have an onclick function of gsftSubmit. This gsftSubmit function is an out of the box function to submit the form page and to instruct the server to perform the code located within that UI action.

This script takes the gsftSubmit function, copies it across to another variable, gsftSubmitHolder, and then re-assigns the gsftSubmit function to a simple function that just alerts the user that the page is still loading.

It then adds a function into the after page loaded queue (for onload script ordering, you can have a look at my post here), which then re-assigns the gsftSubmit function back to its original form, allowing a user to click all the actions again and submit the form.

 

1 Comment

  1. Pingback: Halt submit of form before complete loading – Service Now Mantra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s