Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This script tracks the time it took to assign a task (like an Incident, Change, etc.) by calculating the difference
between when the record was created and when it was assigned (assigned_to was set).
It checks if the assigned_to field has changed and is not empty.
If it's the first time the record is being assigned (u_assignment_time is empty), it captures the current time.
It then calculates the time difference between when the record was created and when it was assigned.
This time difference (in minutes) is stored in a custom field u_time_to_assign.
The goal is to track how long it took for the record to be assigned after creation


## 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),
## the script is being provided to potentially solve some edge cases.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(function executeRule(current, previous /*null when async*/) {

// Only proceed if assigned_to changed AND is not empty/null
if (current.assigned_to.changes() && !gs.nil(current.assigned_to)) {

gs.info("Assigned_to changed and assigned_to is: " + current.assigned_to);

// Only set u_assigned_time if empty
if (!current.u_assigned_time) {

var assignedTime = new GlideDateTime();
current.u_assigned_time = assignedTime;

var createdTime = new GlideDateTime(current.sys_created_on);

var diffMillis = assignedTime.getNumericValue() - createdTime.getNumericValue();
var diffMinutes = diffMillis / (1000 * 60);

gs.info("Time difference in minutes: " + diffMinutes);

// Assuming u_time_to_assign is a string field
current.u_time_to_assign = diffMinutes.toFixed(2) + " minutes";

gs.info("Set u_time_to_assign to: " + current.u_time_to_assign);
} else {
gs.info("u_assigned_time already set: " + current.u_assigned_time);
}
} else {
gs.info("Assigned_to not changed or is empty.");
}

})(current, previous);
Loading