Script to calculate number of days to close a Requested Item using a schedule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2024 08:37 AM
Hello!
There is a Performance Analytics script that calculates the number of days it takes to close a Requested item, "SD.RequestedItem.CloseTime.Days"
(/now/nav/ui/classic/params/target/pa_scripts.do%3Fsys_id%3D4ce47333531831106feaddeeff7b123d%26sysparm_record_target%3Dpa_scripts%26sysparm_record_row%3D2%26sysparm_record_rows%3D2%26sysparm_record_list%3DnameSTARTSWITHsd%255EORDERBYname)
the Script is:
var diff = function(x, y) {
return y.dateNumericValue() - x.dateNumericValue();
};
var days = function(x, y) {
return diff(x, y) / (24 * 60 * 60 * 1000);
};
days(current.opened_at, current.closed_at);
This is used to help calculate the Meantime to Fulfill Requested Items (Utilizing the Indicator of "Total time to fulfil requested item").
Our Request Manager is asking to be able to refine this using Business days, 8-5 weekdays. The schedule we would use has the sysid of "08fcd0830a0a0b2600079f56b1adb9ae"
Creating a new script to use, I have tried using Gemini and Copilot (we do not own Now Assist) to help update the script using a predined schedule. I have not had any success. These scripts are what have been provided back:
Gemini
var diff = function(x, y) {
// Get the schedule record
var schedule = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae");
// Adjust for schedule
var adjustedX = adjustForSchedule(x, schedule);
var adjustedY = adjustForSchedule(y, schedule);
return adjustedY.dateNumericValue() - adjustedX.dateNumericValue();
};
var days = function(x, y) {
return diff(x, y) / (24 * 60 * 60 * 1000);
};
function adjustForSchedule(date, schedule) {
// Check if the date falls outside the schedule
if (!schedule.isActiveAt(date)) {
// Adjust the date to the next active time
var adjustedDate = new GlideDateTime(date);
adjustedDate.nextActiveTime(schedule);
return adjustedDate;
}
return date;
}
days(current.opened_at, current.closed_at);
When I use this script, this is the error I receive in the Job log:
Error during JavaScript evaluation com.snc.pa.dc.ScriptException: JavaScript evaluation returned: NaN in script: var diff = function(x, y)
Here is the CoPilot Script:
// Function to calculate business days between two dates using a schedule
function calculateBusinessDays(startDate, endDate, scheduleId) {
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var resp = plannedTaskAPI.calculateDuration(startDate, endDate, scheduleId);
var response = new JSON().decode(resp);
var duration = response.duration;
var businessDays = duration / (8 * 60 * 60); // Assuming an 8-hour workday
return businessDays;
}
// Define the schedule ID for 'Monday through Friday, 8-5'
var scheduleId = '08fcd0830a0a0b2600079f56b1adb9ae';
// Calculate business days for the current record
var startDate = current.opened_at.getDisplayValue();
var endDate = current.closed_at.getDisplayValue();
var businessDays = calculateBusinessDays(startDate, endDate, scheduleId);
gs.info('Requested Item: ' + current.number + ' - Business Days to Close: ' + businessDays);
When I use this script, this is the error I see in the job log:
Error during JavaScript evaluation: Not all references of "current" are passed in by "arguments" script: // Function to calculate business days between two dates using a schedule
Also, I have read on SN support where it is not recommended to use the SNC.PlannedTaskAPI in scripts.
I am hoping someone could provide guidance as to what the Script should contain.
Thank you in advance!
Jeremy