Prevent Duplicate Submission
Disable the submit button after first click to prevent duplicate record creation from double-clicks or multiple submissions.
onSubmit Table: incident
#onSubmit #validation #duplicate-prevention #submit-button #user-experience #form-control
Script Code
JavaScript
1function onSubmit() {
2 // Check if form is already being submitted
3 if (g_form.isUserSubmitting) {
4 // Already submitting, prevent duplicate
5 alert('This form is already being submitted. Please wait...');
6 return false;
7 }
8
9 // Validation checks before allowing submission
10 var validationErrors = [];
11
12 // Check mandatory fields
13 var requiredFields = [
14 {name: 'short_description', label: 'Short Description'},
15 {name: 'caller_id', label: 'Caller'}
16 ];
17
18 requiredFields.forEach(function(field) {
19 if (!g_form.getValue(field.name)) {
20 validationErrors.push(field.label + ' is required');
21 }
22 });
23
24 // If validation fails, don't proceed
25 if (validationErrors.length > 0) {
26 alert('Please correct the following errors:\n\n' +
27 validationErrors.join('\n'));
28 return false;
29 }
30
31 // Mark form as being submitted
32 g_form.isUserSubmitting = true;
33
34 // Disable submit button and related buttons
35 // Find and disable all submit-type buttons
36 try {
37 // Get all form buttons
38 var buttons = document.querySelectorAll('button[type="submit"], ' +
39 'button[name="submit"], ' +
40 'input[type="submit"]');
41
42 buttons.forEach(function(button) {
43 button.disabled = true;
44 button.style.opacity = '0.5';
45 button.style.cursor = 'not-allowed';
46
47 // Store original text
48 var originalText = button.innerText || button.value;
49 button.setAttribute('data-original-text', originalText);
50
51 // Update button text to show submission in progress
52 if (button.innerText) {
53 button.innerText = 'Submitting...';
54 } else if (button.value) {
55 button.value = 'Submitting...';
56 }
57 });
58
59 // Also try to disable ServiceNow's specific buttons
60 if (typeof gsftSubmit !== 'undefined') {
61 // Disable standard save button
62 var saveButton = gel('sysverb_insert') || gel('sysverb_update');
63 if (saveButton) {
64 saveButton.disabled = true;
65 saveButton.style.opacity = '0.5';
66 }
67 }
68 } catch (e) {
69 console.error('Error disabling buttons:', e);
70 // Continue with submission even if button disable fails
71 }
72
73 // Show loading message
74 g_form.addInfoMessage('Submitting form, please wait...');
75
76 // Set a timeout to re-enable if submission fails
77 setTimeout(function() {
78 // If we're still on the form after 30 seconds, something went wrong
79 if (g_form.isUserSubmitting) {
80 g_form.isUserSubmitting = false;
81
82 // Re-enable buttons
83 var buttons = document.querySelectorAll('button[disabled]');
84 buttons.forEach(function(button) {
85 button.disabled = false;
86 button.style.opacity = '1';
87 button.style.cursor = 'pointer';
88
89 // Restore original text
90 var originalText = button.getAttribute('data-original-text');
91 if (originalText) {
92 if (button.innerText) {
93 button.innerText = originalText;
94 } else if (button.value) {
95 button.value = originalText;
96 }
97 }
98 });
99
100 g_form.clearMessages();
101 g_form.addErrorMessage('Submission timed out. Please try again.');
102 }
103 }, 30000); // 30 second timeout
104
105 // Allow submission to proceed
106 return true;
107}
How to Use
1. Create an onSubmit Client Script on your table
2. Adjust requiredFields array for your mandatory fields
3. Customize timeout value if needed (default: 30 seconds)
4. Test by attempting to double-click submit button
5. Verify button is re-enabled if submission fails
6. Consider adding similar logic to UI Actions
7. Test with slow network connections to ensure UX is good
Related Scripts
Auto-populate Related Fields
Automatically populate fields when a reference field changes (e.g., populate caller's phone and email when caller is selected).
Conditional Mandatory Fields
Make fields mandatory based on the value of another field (e.g., require close notes only when state is Resolved).
Dynamic Field Visibility
Show or hide fields based on another field's value (e.g., show "Other reason" field only when user selects "Other" from dropdown).