Skip to content

Commit 1241a41

Browse files
Calculate Due date using user defined schedules (#1894)
* Create Code.js * Create README.md * Add Business rule: Add or remove tag to the ticket * Rename code.js to code.js * Rename README.md to README.md * Adding new UI Action: Generate PDF * Adding new UI Action: Generate PDF * Adding new Background script: Bulk Update of Fulfillment Group References in Published KB Articles * Adding new Background script: Bulk Update of Fulfillment Group References in Published KB Articles * Delete Server-Side Components/Background Scripts/Bulk Update of Fulfillment Group References in Published KB Articles directory * Update Script.js * Update README.md * Update README.md * Delete Client-Side Components/UI Actions/Generate PDF directory * Create script.js * Create README.md * Create script.js * Create README.md * Delete Server-Side Components/Script Includes/Calculate Due date using user defined schedules directory * Create README.md * Add files via upload * Update script.js * Update README.md
1 parent 5ca3180 commit 1241a41

File tree

2 files changed

+36
-0
lines changed
  • Core ServiceNow APIs/GlideDateTime/Calculate Due date using user defined schedules

2 files changed

+36
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
**Description:**
2+
This Script Include calculates a future due date by adding a specified number of business days to a given start date, based on a defined schedule.
3+
This can be used anywhere within the server side scripts like fix scripts, background scripts, UI Action (server script).
4+
5+
**Pre-requisite:**
6+
A schedule record with valid schedule entries should be created in the cmn_schedule table
7+
A business hours value per day need to be configured
8+
In this sample, the business hours per day is configured as 8 hours i.e 9AM - 5PM.
9+
10+
**Sample:**
11+
var daysToAdd = 4; // No of days need to be added
12+
var script = new CaclculateDueDate().calculateDueDate(new GlideDateTime(),daysToAdd); // Passing the current date and daysToAdd value to script include
13+
gs.print(script);
14+
15+
**Output:**
16+
*** Script: 2025-10-13 13:56:07
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var CaclculateDueDate = Class.create();
2+
CaclculateDueDate.prototype = {
3+
initialize: function() {},
4+
5+
calculateDueDate: function(date, days_to_add) {
6+
var business_hour_per_day = 8; // This can be stored in the system property (Value in Hours) and reused
7+
var duration_script = new DurationCalculator(); // OOB Script include
8+
var tz = gs.getSysTimeZone(); // Get the system timezone
9+
10+
duration_script.setSchedule('c798c1dfc3907e1091ea5242b40131c8', tz); // Sys id of the schedule
11+
duration_script.setStartDateTime(new GlideDateTime(date));
12+
var total_duration = days_to_add * (business_hour_per_day * 60 * 60); // Converting the days to seconds
13+
duration_script.calcDuration(total_duration);
14+
15+
var calculated_due_date = duration_script.getEndDateTime();
16+
return calculated_due_date.getDisplayValue();
17+
},
18+
19+
type: 'CaclculateDueDate'
20+
};

0 commit comments

Comments
 (0)