Skip to content

Commit 40e0bc9

Browse files
authored
Calculate the due date based on the Priority
Calculate the due date based on the Priority
1 parent 544e747 commit 40e0bc9

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
3+
Input
4+
1. Created Date
5+
2. Priority
6+
7+
Output
8+
1. Due Date
9+
10+
Based on Priority equivalent due dates
11+
12+
P1 - add 4hrs to the Created date
13+
P2 - add 4hrs to the Created date but if it's exceed the working hrs of of 5 PM the add to the next day or if the is before the working hours of 8 AM set 5 PM to the same Created date.
14+
P3 or P4 - Kind of low priority so add the due date to the next day but it should exclude the holidays and the weekend's and the populate the next business working day.
15+
16+
*/
17+
18+
19+
// This SI findDueDate() function will help to calculate the duration based on the each priority.
20+
21+
var CalculateDueDates = Class.create();
22+
CalculateDueDates.prototype = {
23+
initialize: function() {},
24+
25+
findDueDate: function(priority, created) {
26+
var dueDateVal;
27+
28+
29+
// For the Priority 1 and adding 4 hours in reagrd less of 8-5 working hours and then holidays
30+
if (priority == 1) {
31+
var now = new GlideDateTime(created);
32+
now.addSeconds(60 * 60 * 4); // Add 4 hours
33+
dueDateVal = now;
34+
return dueDateVal;
35+
36+
}
37+
38+
// For the Priority 2 and adding the 4 hours if exceed the workin hours then add the next day before 5'o Clock
39+
else if (priority == 2) {
40+
var dueDate = new GlideDateTime(created);
41+
dueDate.addSeconds(60 * 60 * 4); // Add 4 hours
42+
dueDate = dueDate+'';
43+
var hours = Number((dueDate + '').slice(11, 13));
44+
45+
if (hours >= 0 && hours < 12) {
46+
gs.addInfoMessage('if Inside 8-5/7');
47+
dueDateVal = dueDate.slice(0, 10) + " 17:00:00";
48+
return dueDateVal;
49+
50+
} else if (hours >= 17 && hours <= 23) {
51+
var nextDate = new GlideDateTime(created);
52+
nextDate.addDaysUTC(1);
53+
var newDue = new GlideDateTime(nextDate.getDate().getValue() + " 17:00:00");
54+
dueDateVal = newDue;
55+
return dueDateVal;
56+
} else {
57+
dueDateVal = dueDate;
58+
return dueDateVal;
59+
}
60+
61+
}
62+
63+
// For the Priority 3 or 4 add the next day and then if the due date is holiday or weekend populate the next working day in a respective field
64+
else if (priority == 3 || priority == 4) {
65+
var schedule = new GlideSchedule();
66+
// cmn_schedule for the Holidays
67+
var scheduleId = 'bd6d74b2c3fc72104f7371edd40131b7';
68+
schedule.load(scheduleId);
69+
70+
var nextDay = new GlideDateTime(created);
71+
nextDay.addDaysUTC(1);
72+
73+
74+
//Checking for weekends
75+
var dayOfWeek = nextDay.getDayOfWeekUTC();
76+
77+
var isWeekend = (dayOfWeek == 6 || dayOfWeek == 7);
78+
79+
80+
// Loop until next working day (weekdays excluding holidays)
81+
while (schedule.isInSchedule(nextDay) || isWeekend) {
82+
nextDay.addDaysUTC(1);
83+
dayOfWeek = nextDay.getDayOfWeekUTC();
84+
isWeekend = (dayOfWeek == 6 || dayOfWeek == 7);
85+
}
86+
87+
// Set to 12:00 PM on that valid day
88+
var validDate = new GlideDateTime(nextDay.getDate().getValue() + " 17:00:00");
89+
return validDate;
90+
}
91+
},
92+
93+
type: 'CalculateDueDates'
94+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Table - incident
3+
Show Update - True
4+
Form Button - True
5+
Condition - (current.due_date == '' && current.priority != '5')
6+
7+
Input
8+
1. Created Date
9+
2. Priority
10+
11+
Validation
12+
Will not appeare if the value is already there and the priority is 5
13+
14+
Output
15+
1. Due Date
16+
17+
*/
18+
19+
20+
// The function duedate is used to pass the priority and then created display value to the script include where the calculate of Due date is done will get the response and the set the value to the due_date field of incident.
21+
function duedate() {
22+
23+
var priority = current.getValue('priority');
24+
var created = current.getDisplayValue('sys_created_on');
25+
var si = new CalculateDueDates();
26+
var response = si.findDueDate(priority, created);
27+
var gdt = new GlideDateTime();
28+
gdt.setDisplayValue(response);
29+
current.setValue('due_date', gdt);
30+
current.update();
31+
action.setRedirectURL(current);
32+
33+
}
34+
duedate();

0 commit comments

Comments
 (0)