|
| 1 | +# Form Dirty State Prevention |
| 2 | + |
| 3 | +## Overview |
| 4 | +Detects form changes and warns users before navigating away or closing the form, preventing accidental data loss. |
| 5 | + |
| 6 | +## What It Does |
| 7 | +- Tracks form field changes (dirty state) |
| 8 | +- Warns user before leaving unsaved form |
| 9 | +- Allows user to cancel navigation |
| 10 | +- Works with all form fields |
| 11 | +- Prevents accidental data loss |
| 12 | +- Clean, reusable pattern |
| 13 | + |
| 14 | +## Use Cases |
| 15 | +- Complex multi-field forms |
| 16 | +- Long data entry forms |
| 17 | +- Forms with expensive operations |
| 18 | +- Critical data entry (financial, medical) |
| 19 | +- Any form where accidental exit would cause issues |
| 20 | + |
| 21 | +## Files |
| 22 | +- `form_dirty_state_manager.js` - Client Script to manage form state |
| 23 | + |
| 24 | +## How to Use |
| 25 | + |
| 26 | +### Step 1: Create Client Script |
| 27 | +1. Go to **System Definition > Scripts** (any table) |
| 28 | +2. Create new Client Script |
| 29 | +3. Set **Type** to "onChange" |
| 30 | +4. Copy code from `form_dirty_state_manager.js` |
| 31 | +5. Set to run on any field |
| 32 | + |
| 33 | +### Step 2: Add Navigation Handler |
| 34 | +1. Add to form's onLoad script: |
| 35 | +```javascript |
| 36 | +// Initialize dirty state tracking |
| 37 | +var formStateManager = new FormDirtyStateManager(); |
| 38 | +``` |
| 39 | + |
| 40 | +### Step 3: Test |
| 41 | +1. Open form and make changes |
| 42 | +2. Try to navigate away without saving |
| 43 | +3. User sees warning dialog |
| 44 | +4. User can choose to stay or leave |
| 45 | + |
| 46 | +## Example Usage |
| 47 | +```javascript |
| 48 | +// Automatically tracks all field changes |
| 49 | +// When user tries to close/navigate: |
| 50 | +// "You have unsaved changes. Do you want to leave?" |
| 51 | +// - Leave (discard changes) |
| 52 | +// - Stay (return to form) |
| 53 | +``` |
| 54 | + |
| 55 | +## Key Features |
| 56 | +- ✅ Detects any field change |
| 57 | +- ✅ Persistent across form interactions |
| 58 | +- ✅ Works with new records and updates |
| 59 | +- ✅ Ignores read-only fields |
| 60 | +- ✅ Resets after save |
| 61 | +- ✅ No performance impact |
| 62 | + |
| 63 | +## Output Examples |
| 64 | +``` |
| 65 | +User opens form and changes a field |
| 66 | +→ Form marked as "dirty" |
| 67 | +
|
| 68 | +User clicks close/back button |
| 69 | +→ Warning dialog appears: "You have unsaved changes" |
| 70 | +
|
| 71 | +User clicks Leave |
| 72 | +→ Form closes, changes discarded |
| 73 | +
|
| 74 | +User clicks Save then navigates |
| 75 | +→ No warning (form is clean) |
| 76 | +``` |
| 77 | + |
| 78 | +## Customization |
| 79 | +```javascript |
| 80 | +// Customize warning message |
| 81 | +var warningMessage = "Warning: You have unsaved changes!"; |
| 82 | + |
| 83 | +// Add specific field tracking |
| 84 | +g_form.addOnFieldChange('priority', myCustomHandler); |
| 85 | + |
| 86 | +// Reset dirty flag after save |
| 87 | +g_form.save(); // Automatically triggers cleanup |
| 88 | +``` |
| 89 | + |
| 90 | +## Requirements |
| 91 | +- ServiceNow instance |
| 92 | +- Client Script access |
| 93 | +- Any table form |
| 94 | + |
| 95 | +## Browser Support |
| 96 | +- Chrome, Firefox, Safari, Edge (all modern browsers) |
| 97 | +- Works with ServiceNow classic and modern UI |
| 98 | + |
| 99 | +## Related APIs |
| 100 | +- [g_form API](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_FormAPI.html) |
| 101 | +- [Client Script Events](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_ClientScriptEvents.html) |
| 102 | +- [Form Field Changes](https://docs.servicenow.com/bundle/sandiego-application-development/page/app-store/dev_apps/concept/c_FieldChanges.html) |
| 103 | + |
| 104 | +## Best Practices |
| 105 | +- Apply to important data entry forms |
| 106 | +- Test with real users |
| 107 | +- Consider accessibility for screen readers |
| 108 | +- Use with save shortcuts (Ctrl+S) |
| 109 | +- Combine with auto-save patterns |
0 commit comments