← Script library

Client Scripts

Prevent Duplicate Submission

Disable the submit button after first click to prevent duplicate record creation from double-clicks or multiple submissions.

JavaScript
function onSubmit() {
  // Check if form is already being submitted
  if (g_form.isUserSubmitting) {
    // Already submitting, prevent duplicate
    alert('This form is already being submitted. Please wait...');
    return false;
  }

  // Validation checks before allowing submission
  var validationErrors = [];

  // Check mandatory fields
  var requiredFields = [
    {name: 'short_description', label: 'Short Description'},
    {name: 'caller_id', label: 'Caller'}
  ];

  requiredFields.forEach(function(field) {
    if (!g_form.getValue(field.name)) {
      validationErrors.push(field.label + ' is required');
    }
  });

  // If validation fails, don't proceed
  if (validationErrors.length > 0) {
    alert('Please correct the following errors:\n\n' + 
          validationErrors.join('\n'));
    return false;
  }

  // Mark form as being submitted
  g_form.isUserSubmitting = true;

  // Disable submit button and related buttons
  // Find and disable all submit-type buttons
  try {
    // Get all form buttons
    var buttons = document.querySelectorAll('button[type="submit"], ' +
                                            'button[name="submit"], ' +
                                            'input[type="submit"]');
    
    buttons.forEach(function(button) {
      button.disabled = true;
      button.style.opacity = '0.5';
      button.style.cursor = 'not-allowed';
      
      // Store original text
      var originalText = button.innerText || button.value;
      button.setAttribute('data-original-text', originalText);
      
      // Update button text to show submission in progress
      if (button.innerText) {
        button.innerText = 'Submitting...';
      } else if (button.value) {
        button.value = 'Submitting...';
      }
    });

    // Also try to disable ServiceNow's specific buttons
    if (typeof gsftSubmit !== 'undefined') {
      // Disable standard save button
      var saveButton = gel('sysverb_insert') || gel('sysverb_update');
      if (saveButton) {
        saveButton.disabled = true;
        saveButton.style.opacity = '0.5';
      }
    }
  } catch (e) {
    console.error('Error disabling buttons:', e);
    // Continue with submission even if button disable fails
  }

  // Show loading message
  g_form.addInfoMessage('Submitting form, please wait...');

  // Set a timeout to re-enable if submission fails
  setTimeout(function() {
    // If we're still on the form after 30 seconds, something went wrong
    if (g_form.isUserSubmitting) {
      g_form.isUserSubmitting = false;
      
      // Re-enable buttons
      var buttons = document.querySelectorAll('button[disabled]');
      buttons.forEach(function(button) {
        button.disabled = false;
        button.style.opacity = '1';
        button.style.cursor = 'pointer';
        
        // Restore original text
        var originalText = button.getAttribute('data-original-text');
        if (originalText) {
          if (button.innerText) {
            button.innerText = originalText;
          } else if (button.value) {
            button.value = originalText;
          }
        }
      });
      
      g_form.clearMessages();
      g_form.addErrorMessage('Submission timed out. Please try again.');
    }
  }, 30000);  // 30 second timeout

  // Allow submission to proceed
  return true;
}

How to use it

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

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.