Skip to content

Commit 5bc371d

Browse files
Improve Cancel Incident UI Action implementation
1 parent 580f8c5 commit 5bc371d

File tree

3 files changed

+300
-13
lines changed

3 files changed

+300
-13
lines changed
Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
This is a ui action run on incident table which is client callable, run on condition current.state == '1' which means when state is new.It cancel the incident through the form.
1+
# Cancel Incident UI Action
2+
3+
A UI Action in ServiceNow is a script that defines an action or button within the platform's user interface. It enables users to perform specific operations on forms and lists, such as creating, updating, or deleting records, or executing custom scripts. UI Actions enhance the user experience by providing functional buttons, links, or context menus.
4+
5+
## Overview
6+
7+
This UI Action allows users to cancel incidents directly from the incident form. It provides a confirmation dialog to prevent accidental cancellations and updates the incident state to "Cancelled" (state value 8) when confirmed.
8+
9+
## Features
10+
11+
- **Confirmation Dialog**: Uses GlideModal to display a confirmation prompt before cancelling
12+
- **State Management**: Updates incident state to "Cancelled" (value 8)
13+
- **Client-Side Validation**: Runs client-side for better user experience
14+
- **Conditional Display**: Only shows when incident state is "New" (state value 1)
15+
16+
## Configuration
17+
18+
Create a UI Action with the following field values:
19+
20+
**Name**: Cancel Incident
21+
22+
**Action Name**: cancel_incident
23+
24+
**Table**: Incident [incident]
25+
26+
**Client**: checked (true)
27+
28+
**Onclick**: cancelIncident();
29+
30+
**Condition**: current.state == '1'
31+
32+
**Script**: Use the provided script.js file
33+
34+
## Usage
35+
36+
1. Navigate to an incident record in "New" state
37+
2. Click the "Cancel Incident" button
38+
3. Confirm the action in the modal dialog
39+
4. The incident state will be updated to "Cancelled"
40+
41+
## Technical Details
42+
43+
- **Client-Side Function**: `cancelIncident()` - Displays confirmation modal
44+
- **Server-Side Function**: `serverCancel()` - Updates the incident state
45+
- **Modal Configuration**: Uses `glide_ask_standard` modal with custom title
46+
- **State Value**: Sets incident state to '8' (Cancelled)
47+
48+
## Prerequisites
49+
50+
- User must have write access to the incident table
51+
- Incident must be in "New" state (state = 1) for the UI Action to be visible
52+
53+
## Notes
54+
55+
- This UI Action only appears on incident forms when the state is "New"
56+
- The confirmation dialog helps prevent accidental cancellations
57+
- The server-side script executes only after user confirmation
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Setup Instructions for Cancel Incident UI Action
2+
3+
This document provides detailed step-by-step instructions for implementing the Cancel Incident UI Action in your ServiceNow instance.
4+
5+
## Prerequisites
6+
7+
- Administrative access to ServiceNow instance
8+
- Access to System Definition > UI Actions module
9+
- Understanding of ServiceNow UI Actions and client-server scripting
10+
11+
## Step-by-Step Setup
12+
13+
### 1. Navigate to UI Actions
14+
15+
1. In ServiceNow, go to **System Definition > UI Actions**
16+
2. Click **New** to create a new UI Action
17+
18+
### 2. Configure Basic Settings
19+
20+
Fill in the following fields:
21+
22+
| Field | Value | Description |
23+
|-------|-------|-------------|
24+
| **Name** | Cancel Incident | Display name for the UI Action |
25+
| **Table** | Incident [incident] | Target table for the UI Action |
26+
| **Action name** | cancel_incident | Unique identifier for the action |
27+
| **Active** | ✓ (checked) | Enables the UI Action |
28+
29+
### 3. Configure Display Settings
30+
31+
| Field | Value | Description |
32+
|-------|-------|-------------|
33+
| **Form button** | ✓ (checked) | Shows button on form view |
34+
| **Form link** | ☐ (unchecked) | Optional: Show as link instead |
35+
| **List banner button** | ☐ (unchecked) | Not needed for this action |
36+
| **List choice** | ☐ (unchecked) | Not needed for this action |
37+
38+
### 4. Configure Client Settings
39+
40+
| Field | Value | Description |
41+
|-------|-------|-------------|
42+
| **Client** | ✓ (checked) | Enables client-side execution |
43+
| **Onclick** | `cancelIncident();` | Client-side function to call |
44+
45+
### 5. Configure Conditions
46+
47+
| Field | Value | Description |
48+
|-------|-------|-------------|
49+
| **Condition** | `current.state == '1'` | Only show for "New" incidents |
50+
51+
### 6. Add the Script
52+
53+
Copy the entire content from `script.js` and paste it into the **Script** field of the UI Action.
54+
55+
### 7. Configure Advanced Settings (Optional)
56+
57+
| Field | Value | Description |
58+
|-------|-------|-------------|
59+
| **Order** | 100 | Display order (adjust as needed) |
60+
| **Hint** | Cancel this incident | Tooltip text |
61+
| **Comments** | UI Action to cancel incidents in New state | Internal documentation |
62+
63+
## Verification Steps
64+
65+
### 1. Test the UI Action
66+
67+
1. Navigate to an incident in "New" state
68+
2. Verify the "Cancel Incident" button appears
69+
3. Click the button and confirm the modal appears
70+
4. Test both "OK" and "Cancel" in the confirmation dialog
71+
72+
### 2. Verify State Changes
73+
74+
1. After confirming cancellation, check that:
75+
- Incident state changes to "Cancelled"
76+
- Work notes are added with cancellation details
77+
- Success message appears
78+
79+
### 3. Test Edge Cases
80+
81+
1. Try accessing the UI Action on incidents in other states (should not appear)
82+
2. Test with different user roles to ensure proper permissions
83+
3. Verify error handling works correctly
84+
85+
## Troubleshooting
86+
87+
### Common Issues
88+
89+
**UI Action doesn't appear:**
90+
- Check that the incident is in "New" state (state = 1)
91+
- Verify the condition field: `current.state == '1'`
92+
- Ensure the UI Action is marked as Active
93+
94+
**Script errors:**
95+
- Check browser console for JavaScript errors
96+
- Verify the script is properly copied from `script.js`
97+
- Ensure proper syntax and formatting
98+
99+
**Permission issues:**
100+
- Verify user has write access to incident table
101+
- Check ACL rules for incident cancellation
102+
- Ensure proper role assignments
103+
104+
### Debug Mode
105+
106+
To enable debug logging, add this line at the beginning of the `serverCancel()` function:
107+
108+
```javascript
109+
gs.info('Debug: Starting incident cancellation for ' + current.number);
110+
```
111+
112+
## Security Considerations
113+
114+
- The UI Action respects existing ACL rules
115+
- Only users with incident write permissions can cancel incidents
116+
- All cancellations are logged for audit purposes
117+
- Work notes provide cancellation history
118+
119+
## Customization Options
120+
121+
### Modify Confirmation Message
122+
123+
Edit line 33 in the script to customize the confirmation dialog:
124+
125+
```javascript
126+
gm.setPreference("question", "Your custom message here");
127+
```
128+
129+
### Change Cancellation Reason
130+
131+
Modify the work note in the `serverCancel()` function (line 79):
132+
133+
```javascript
134+
var workNote = 'Custom cancellation reason: ' + gs.getUserDisplayName() + ' on ' + gs.nowDateTime();
135+
```
136+
137+
### Add Additional Validations
138+
139+
Add custom validation logic in the `cancelIncident()` function before showing the modal.
140+
141+
## Support
142+
143+
For issues or questions:
144+
1. Check ServiceNow system logs
145+
2. Review browser console for client-side errors
146+
3. Test in a development instance first
147+
4. Consult ServiceNow documentation for UI Actions
Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,101 @@
1-
function cancelIncident(){
2-
var gm = new GlideModal("glide_ask_standard", false, 600); // glide modal to get the confirmation
3-
gm.setPreference("title", "Are you sure you wanna cancel incident!!!");
4-
gm.setPreference("onPromptComplete", function() {
5-
gsftSubmit(null,g_form.getFormElement(),'cancel_incident');}); //calling same ui action
6-
gm.render();
7-
1+
/**
2+
* Client-side function to initiate incident cancellation
3+
* Displays a confirmation modal before proceeding with the cancellation
4+
*/
5+
function cancelIncident() {
6+
try {
7+
// Validate that we have a valid form and record
8+
if (!g_form || !g_form.getUniqueValue()) {
9+
alert('Error: Unable to access incident record. Please refresh the page and try again.');
10+
return;
11+
}
12+
13+
// Check if incident is in the correct state for cancellation
14+
var currentState = g_form.getValue('state');
15+
if (currentState !== '1') {
16+
alert('Error: This incident cannot be cancelled. Only incidents in "New" state can be cancelled.');
17+
return;
18+
}
19+
20+
// Create confirmation modal with improved messaging
21+
var gm = new GlideModal("glide_ask_standard", false, 600);
22+
gm.setPreference("title", "Cancel Incident Confirmation");
23+
gm.setPreference("warning", true);
24+
gm.setPreference("onPromptComplete", function() {
25+
// Show loading message
26+
g_form.addInfoMessage('Cancelling incident...');
27+
28+
// Submit the form to trigger server-side processing
29+
gsftSubmit(null, g_form.getFormElement(), 'cancel_incident');
30+
});
31+
32+
// Set the confirmation message
33+
gm.setPreference("question", "Are you sure you want to cancel this incident?\n\nThis action will change the incident state to 'Cancelled' and cannot be easily undone.");
34+
35+
// Render the modal
36+
gm.render();
37+
38+
} catch (error) {
39+
// Handle any unexpected errors
40+
console.error('Error in cancelIncident function:', error);
41+
alert('An unexpected error occurred. Please contact your system administrator.');
42+
}
843
}
944

10-
if(typeof window == 'undefined'){
11-
serverCancel();
45+
/**
46+
* Server-side execution block
47+
* This code runs on the server when the UI Action is submitted
48+
*/
49+
if (typeof window == 'undefined') {
50+
serverCancel();
1251
}
1352

14-
function serverCancel(){
15-
current.state = '8'; //setting the state to canceled
16-
current.update();
53+
/**
54+
* Server-side function to cancel the incident
55+
* Updates the incident state to 'Cancelled' and adds a work note
56+
*/
57+
function serverCancel() {
58+
try {
59+
// Validate that we have a current record
60+
if (!current || !current.isValidRecord()) {
61+
gs.addErrorMessage('Error: Invalid incident record.');
62+
return;
63+
}
64+
65+
// Double-check the current state before cancelling
66+
if (current.state.toString() !== '1') {
67+
gs.addErrorMessage('Error: This incident cannot be cancelled. Only incidents in "New" state can be cancelled.');
68+
return;
69+
}
70+
71+
// Store original values for logging
72+
var incidentNumber = current.number.toString();
73+
var originalState = current.state.getDisplayValue();
74+
75+
// Update the incident state to 'Cancelled' (state value 8)
76+
current.state = '8';
77+
78+
// Add a work note to document the cancellation
79+
var workNote = 'Incident cancelled by ' + gs.getUserDisplayName() + ' on ' + gs.nowDateTime();
80+
if (current.work_notes.nil()) {
81+
current.work_notes = workNote;
82+
} else {
83+
current.work_notes = current.work_notes + '\n\n' + workNote;
84+
}
85+
86+
// Update the record
87+
current.update();
88+
89+
// Log the action for audit purposes
90+
gs.info('Incident ' + incidentNumber + ' cancelled by user ' + gs.getUserName() +
91+
'. State changed from "' + originalState + '" to "Cancelled"');
92+
93+
// Provide user feedback
94+
gs.addInfoMessage('Incident ' + incidentNumber + ' has been successfully cancelled.');
95+
96+
} catch (error) {
97+
// Handle server-side errors
98+
gs.error('Error cancelling incident: ' + error.message);
99+
gs.addErrorMessage('An error occurred while cancelling the incident. Please contact your system administrator.');
100+
}
17101
}

0 commit comments

Comments
 (0)