Skip to content

Commit 13e7377

Browse files
authored
Captures the time it took to assign a task (#1830)
* Create script.js * Create README.md
1 parent 2b1ed07 commit 13e7377

File tree

2 files changed

+44
-0
lines changed
  • Server-Side Components/Business Rules/Captures the time it took to assign a task

2 files changed

+44
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This script tracks the time it took to assign a task (like an Incident, Change, etc.) by calculating the difference
2+
between when the record was created and when it was assigned (assigned_to was set).
3+
It checks if the assigned_to field has changed and is not empty.
4+
If it's the first time the record is being assigned (u_assignment_time is empty), it captures the current time.
5+
It then calculates the time difference between when the record was created and when it was assigned.
6+
This time difference (in minutes) is stored in a custom field u_time_to_assign.
7+
The goal is to track how long it took for the record to be assigned after creation
8+
9+
10+
## While this is possible to do via Metrics in ServiceNow (https://www.servicenow.com/docs/bundle/xanadu-platform-administration/page/use/reporting/concept/c_SampleFieldValueDurationScript.html),
11+
## the script is being provided to potentially solve some edge cases.
12+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// Only proceed if assigned_to changed AND is not empty/null
4+
if (current.assigned_to.changes() && !gs.nil(current.assigned_to)) {
5+
6+
gs.info("Assigned_to changed and assigned_to is: " + current.assigned_to);
7+
8+
// Only set u_assigned_time if empty
9+
if (!current.u_assigned_time) {
10+
11+
var assignedTime = new GlideDateTime();
12+
current.u_assigned_time = assignedTime;
13+
14+
var createdTime = new GlideDateTime(current.sys_created_on);
15+
16+
var diffMillis = assignedTime.getNumericValue() - createdTime.getNumericValue();
17+
var diffMinutes = diffMillis / (1000 * 60);
18+
19+
gs.info("Time difference in minutes: " + diffMinutes);
20+
21+
// Assuming u_time_to_assign is a string field
22+
current.u_time_to_assign = diffMinutes.toFixed(2) + " minutes";
23+
24+
gs.info("Set u_time_to_assign to: " + current.u_time_to_assign);
25+
} else {
26+
gs.info("u_assigned_time already set: " + current.u_assigned_time);
27+
}
28+
} else {
29+
gs.info("Assigned_to not changed or is empty.");
30+
}
31+
32+
})(current, previous);

0 commit comments

Comments
 (0)