Skip to content

Commit 338a7b0

Browse files
Merge branch 'ServiceNowDevProgram:main' into attaching-attachment-to-record
2 parents 91a03d2 + 1106276 commit 338a7b0

File tree

13 files changed

+286
-0
lines changed

13 files changed

+286
-0
lines changed
96.8 KB
Loading
75.2 KB
Loading
93.9 KB
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Automatically Populate Incident with Work Order Number, State , OnHold Reason using business rule
2+
3+
This use case automates the linkage between **Work Orders** and **Incidents** in ServiceNow. When a **Work Order** is created from an **Incident**, the system automatically :
4+
- Populates the Work Order reference (**u_work_order**) field on the parent Incident
5+
- Sets the **Incident State → On Hold**
6+
- Updates the **Hold Reason → Awaiting Field Work Order**
7+
- Adds a **work note** in the Activity Stream for traceability
8+
9+
This ensures a seamless connection between **ITSM** and **Field Service Management (FSM)** processes, allowing to identify related Work Orders without navigating to related lists.
10+
11+
## Why This Use Case
12+
Out-of-the-box (OOB) ServiceNow does **not** include a direct Work Order reference field on the Incident form.
13+
By default, users can only view Work Orders under the *Related Tasks → Work Orders* section, which limits quick visibility.
14+
15+
To address this gap, a custom reference field (**u_work_order**) was created under the **Related Records** section ( similar to *Problem* or *Parent Incident* ).
16+
- Displays the linked Work Order number directly on the Incident form
17+
- Prevents manual modification while maintaining clear visibility for agents
18+
19+
## Process Alignment
20+
In standard ITSM workflows, Incidents can be placed **On Hold** for reasons such as *Awaiting Problem*, *Awaiting Change*, or *Awaiting Vendor*. Since there was no default option for Field Service, this customization introduces a new Hold Reason : **Awaiting Field Work Order**
21+
22+
When a Work Order is created from an Incident:
23+
- The Incident automatically moves to **On Hold**
24+
- The **Hold Reason** updates to *Awaiting Field Work Order*
25+
- A **work note** logs the Activity Stream
26+
27+
This behavior aligns with existing ITSM hold logic for Problem and Change processes
28+
29+
## Prerequisites
30+
31+
#### Ensure FSM Application Plugin is Installed
32+
- This customization requires the Field Service Management (FSM) plugin to be installed and active in ServiceNow instance
33+
- Search for Field Service Management plugin **com.snc.work_management** and install
34+
- FSM provides the **Work Order [wm_order]** table
35+
36+
#### Create a Custom Field on the Incident Table
37+
- Create Custom Reference Field on Incident table with label name **Work Order** [u_work_order] and select Type as **Reference**
38+
- Reference to Table Work Order [wm_order]
39+
- Save and add this field to the **Incident form** under the **Related Records** section
40+
41+
#### Add Custom Choice in On Hold Reason Field
42+
- Navigate to hold_reason field on Incident table
43+
- Configure dictionary and add **Awaiting Field Work Order** with value set as 2 and sequence as 5
44+
- Save the new choice to make it selectable in the *Hold Reason* field.
45+
46+
---
47+
### Creating an Incident
48+
![Create_Incident](Populate_WorkOrder_Number_1.png)
49+
50+
---
51+
52+
### Work Order Created from Incident
53+
![WorkOrder_From_Incident](Populate_WorkOrder_Number_2.png)
54+
55+
---
56+
57+
### Incident Updated with Work Order Reference, State On Hold, and Reason Awaiting Field Work Order
58+
![Populate_WorkOrderNumber_State_onHoldReason](Populate_WorkOrder_Number_3.png)
59+
60+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
if (!current.initiated_from.nil()) {
4+
5+
var inc = new GlideRecord('incident');
6+
if (inc.get(current.initiated_from)) {
7+
8+
// Link the Work Order reference to the Incident
9+
inc.u_work_order = current.sys_id;
10+
11+
// Move the Incident to On Hold and set Hold Reason
12+
inc.state = 3; // 3 = On Hold
13+
inc.hold_reason = '2'; // Awaiting Field Work Order (custom choice value created)
14+
15+
// Add work note
16+
inc.work_notes = 'Work Order ' + current.number + ' has been created and Incident moved to On Hold.';
17+
18+
inc.update();
19+
}
20+
}
21+
22+
})(current, previous);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Scheduled Script Execution: Auto-cancel RITM if group manager approval pending after 30 days
2+
3+
This script:
4+
- Finds RITM records older than 30 days
5+
- Checks if any group manager approvals are still pending (state='requested')
6+
- If so, cancels the RITM (sets state to 'Cancelled')
7+
Usage:
8+
- Schedule this script to run daily.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Scheduled Script Execution: Auto-cancel RITM if group manager approval pending after 30 days
3+
4+
This script:
5+
- Finds RITM records older than 30 days
6+
- Checks if any group manager approvals are still pending (state='requested')
7+
- If so, cancels the RITM (sets state to 'Cancelled')
8+
Usage:
9+
- Schedule this script to run daily.
10+
*/
11+
12+
// Calculate date 30 days ago
13+
var thirtyDaysAgo = new GlideDateTime();
14+
thirtyDaysAgo.addDaysUTC(-30);
15+
16+
// Query RITMs older than 30 days and not closed/cancelled already
17+
var ritmGR = new GlideRecord('sc_req_item');
18+
ritmGR.addQuery('sys_created_on', '<=', thirtyDaysAgo);
19+
ritmGR.addEncodedQuery('stateIN1,2,112^cat_item=a24b1e113bc21e1050109c9c24e45a51');
20+
ritmGR.query();
21+
22+
while (ritmGR.next()) {
23+
// Query approvals for this RITM from group managers - adjust condition accordingly
24+
var approvalGR = new GlideRecord('sysapproval_approver');
25+
approvalGR.addQuery('sysapproval', ritmGR.sys_id); // approvals linked to this RITM
26+
approvalGR.addQuery('state', 'requested'); // pending approvals
27+
approvalGR.query();
28+
29+
if (approvalGR.hasNext()) {
30+
// Group manager approvals pending after 30 days => Cancel RITM
31+
32+
ritmGR.state = 8; // Closed Cancelled
33+
ritmGR.assignment_group = '<assignment_group_name or sysid>'; //group ABC
34+
ritmGR.work_notes = 'Auto-cancelled due to no group manager approval within 30 days.';
35+
ritmGR.update();
36+
37+
}
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This script will identify all the retired CI and update the releationship by removing it from the CIs
2+
This is will query the cmdb_ci_rel table and fetch all ci with status as install status == 7 and parent install status == 7
3+
4+
As result it will delete all CI relationship and update the delete entry by querying custom table u_deleteret_app
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//get all relationship records where retired CI is Parent
2+
var gr = new GlideRecord('cmdb_rel_ci');
3+
gr.addEncodedQuery("child.install_status=7^ORparent.install_status=7");
4+
gr.query();
5+
6+
//For each record with a retired CI
7+
while (gr.next()) {
8+
var par = gr.parent;
9+
var child = gr.child;
10+
var tp = gr.type;
11+
gr.deleteRecord();
12+
13+
var gr1 = new GlideRecord('u_delete_retired_relationships');
14+
gr1.initialize();
15+
gr1.u_child = child;
16+
gr1.u_parent = par;
17+
gr1.u_type = tp;
18+
gr1.insert();
19+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Attachment Downloads Logger
2+
3+
A ServiceNow utility that logs attachment download activities by adding comments to the work notes of the associated record, enhancing visibility and ensuring compliance.
4+
5+
## Challenge
6+
7+
Tracking attachment downloads in ServiceNow can be critical for maintaining data security and compliance. Without proper logging, it becomes difficult to identify who accessed sensitive information and when. This utility addresses this challenge by providing an automated solution to log attachment download activities directly in the work notes of the record.
8+
9+
This tool is particularly useful in scenarios where data access needs to be monitored for compliance purposes or to ensure accountability in data handling.
10+
11+
## Description
12+
13+
The Attachments Download Logger Script Action is designed to automatically add work note of a record whenever an attachment is downloaded. This ensures that all attachment access activities are logged, providing a clear audit trail for administrators and compliance officers.
14+
15+
## Functionality
16+
17+
The Attachments Download Logger Script Action provides the following capabilities:
18+
- Automatically logs attachment download activities in the work notes of the associated record.
19+
- Captures details such as the user who downloaded the attachment and the timestamp and what attachment was downloaded.
20+
- Enhances visibility into data access activities for better compliance and accountability.
21+
- Operates seamlessly in the background without requiring manual intervention.
22+
23+
## Usage Instructions
24+
25+
26+
### Creating the Script Action
27+
28+
To create the Script Action for the Attachment Download Logger, follow these steps:
29+
30+
1. Navigate to **System Policy > Events > Script Actions** in your ServiceNow instance.
31+
2. Click on the **New** button to create a new Script Action.
32+
3. Fill in the following fields:
33+
34+
- **Name**: `Add worknote for attachment download`
35+
- **Event Name**: `attachment.read`
36+
- **Active**: `true`
37+
- **Order**: `100` (or any appropriate order value based on your instance configuration)
38+
- **Script**:
39+
```javascript
40+
//Add the attached script action code
41+
42+
```
43+
44+
4. Click **Submit** to save the Script Action.
45+
46+
This Script Action will now automatically log attachment download activities in the work notes of the associated record.
47+
48+
49+
50+
### Customizations
51+
52+
You can customize the script to include additional details, such as the IP address of the user or the reason for the download. Additionally, you can restrict the logging functionality to specific tables or user roles based on your requirements.
53+
54+
If you want to restrict the logging functionality to a specific table, you can use the `current.table_name` property in your script. For example, to apply the logging only to the `incident` table, you can add the condition in the **condition Script** field as below.
55+
56+
```javascript
57+
current.table_name == 'incident'
58+
```
59+
60+
This ensures that the logging functionality is executed only for records in the `incident` table.
61+
62+
63+
## Category
64+
65+
Server-Side Components / Script Actions / Attachment Downloads Logger
66+
67+
## Screenshots
68+
<img width="1256" height="132" alt="2025-10-23_22-57-59" src="https://github.com/user-attachments/assets/dbd95461-2b81-40d8-9425-f3c98e724dd1" />

0 commit comments

Comments
 (0)