Skip to Content

Confirmation Dialog Before Save

Show a confirmation dialog before saving the form when certain conditions are met (e.g., warn user about critical changes).

onSubmit Table: incident
#confirmation #onSubmit #dialog #user-warning #validation

Script Code

JavaScript
1function onSubmit() {
2  // Configuration: Define when to show confirmation
3  var showConfirmation = false;
4  var confirmMessage = '';
5
6  // Example 1: Warn when closing without resolution
7  var state = g_form.getValue('state');
8  var closeCode = g_form.getValue('close_code');
9  var closeNotes = g_form.getValue('close_notes');
10
11  if (state === '6' || state === '7') {  // Resolved or Closed
12    if (!closeCode || closeCode === '') {
13      showConfirmation = true;
14      confirmMessage = 'You are closing this incident without a close code. Are you sure you want to continue?';
15    }
16  }
17
18  // Example 2: Warn when changing priority on high-impact incident
19  // Uncomment to enable
20  /*
21  var priority = g_form.getValue('priority');
22  var impact = g_form.getValue('impact');
23  var urgency = g_form.getValue('urgency');
24
25  if (impact === '1' && priority !== '1') {
26    showConfirmation = true;
27    confirmMessage = 'This is a high-impact incident but priority is not Critical. Continue anyway?';
28  }
29  */
30
31  // Example 3: Warn when reassigning multiple times
32  // Uncomment to enable
33  /*
34  var reassignmentCount = g_form.getValue('reassignment_count');
35  if (parseInt(reassignmentCount) > 3) {
36    showConfirmation = true;
37    confirmMessage = 'This incident has been reassigned ' + reassignmentCount + ' times. Are you sure you want to save?';
38  }
39  */
40
41  // Example 4: Warn about empty work notes
42  // Uncomment to enable
43  /*
44  var workNotes = g_form.getValue('work_notes');
45  var stateChanged = g_form.hasChanged('state');
46
47  if (stateChanged && (!workNotes || workNotes === '')) {
48    showConfirmation = true;
49    confirmMessage = 'You are changing the state without adding work notes. Do you want to continue?';
50  }
51  */
52
53  // Show confirmation dialog if conditions are met
54  if (showConfirmation) {
55    var confirmed = confirm(confirmMessage);
56
57    if (!confirmed) {
58      // User clicked Cancel - prevent form submission
59      return false;
60    }
61  }
62
63  // Allow form submission
64  return true;
65}

How to Use

1. Create an onSubmit Client Script on your table 2. Customize the conditions for showing the confirmation 3. Update the confirmation messages for your use case 4. Uncomment additional examples as needed 5. Test by attempting to save the form under various conditions

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts