diff --git a/Client-Side Components/UI Actions/Populate Due Date based on Priority/Readme.md b/Client-Side Components/UI Actions/Populate Due Date based on Priority/Readme.md new file mode 100644 index 0000000000..46a18f1fb3 --- /dev/null +++ b/Client-Side Components/UI Actions/Populate Due Date based on Priority/Readme.md @@ -0,0 +1,47 @@ +**Calculate the due date based on the Priority** + +Script Type: UI Action, Table: incident, Form button: True, Show update: True, Condition: (current.due_date == '' && current.priority != '5'), OnClick: functionName() + +Script Type: Script Include, Glide AJAX enabled: False + +Schedule- Name: Holidays, Time Zone: GMT + +Schedule Entry - Name: New Year's Day, Type: Exclude, Show as: Busy, When: 31-12-2024, To: 01-01-2025 +Schedule Entry - Name: Christmas Day, Type: Exclude, Show as: Busy, When: 24-12-2025, To: 25-12-2025 +Schedule Entry - Name: Thanksgiving Day, Type: Exclude, Show as: Busy, When: 26-11-2025, To: 27-11-2025 +Schedule Entry - Name: Diwali, Type: Exclude, Show as: Busy, When: 19-10-2025, To: 20-10-2025 + +**Goal:** To Calculate Due-Date based on Priority with some conditions. + +**Walk through of code:** So in this use case the UI Action is been used and then Script Include for server calculate is used.So the main to calculate the due-date by the user trigger. + +UI Action- So this button will check the priority and check the due date field is empty or not if not then will fetch the value of "Priority" and "Created date" and pass the data to the Script Include for calculation once it gets the response will populate the value to the due_date field in the incident table and then update it. + +Script Include- The role of this is to get the "Priority" and "Created date" based on prioriy this will calculate the time and date by using th GlidDateTime API and the will do some additional changes based on each priorit which is mentioned below and then return the response back to the UI Action, + +Schedule & Schedule Entry- It is used for the P3 and P4 Priority which is mentioned below for the use case.To exclude the Holidays. + +These are the use case which the above functionality works, + +**1-> P1** - add 4hrs to the Created date +**2-> 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. +**3-> 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. +**4-> P5 **- User manually will populate the due date based on the process. + +The UI Action on the Incident Form +Button + +UI Action which will call the Script Include +UI Action + +Script Include +SI + +Schedules and Schedule Entry +Schedules +Schedule Entry + +Output +Priority 1 +Priority 2 +Priority 4 diff --git a/Client-Side Components/UI Actions/Populate Due Date based on Priority/ScriptInclude.js b/Client-Side Components/UI Actions/Populate Due Date based on Priority/ScriptInclude.js new file mode 100644 index 0000000000..4390ed5633 --- /dev/null +++ b/Client-Side Components/UI Actions/Populate Due Date based on Priority/ScriptInclude.js @@ -0,0 +1,94 @@ +/* + +Input +1. Created Date +2. Priority + +Output +1. Due Date + +Based on Priority equivalent due dates + +P1 - add 4hrs to the Created date +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. +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. + +*/ + + +// This SI findDueDate() function will help to calculate the duration based on the each priority. + +var CalculateDueDates = Class.create(); +CalculateDueDates.prototype = { + initialize: function() {}, + + findDueDate: function(priority, created) { + var dueDateVal; + + + // For the Priority 1 and adding 4 hours in reagrd less of 8-5 working hours and then holidays + if (priority == 1) { + var now = new GlideDateTime(created); + now.addSeconds(60 * 60 * 4); // Add 4 hours + dueDateVal = now; + return dueDateVal; + + } + + // For the Priority 2 and adding the 4 hours if exceed the workin hours then add the next day before 5'o Clock + else if (priority == 2) { + var dueDate = new GlideDateTime(created); + dueDate.addSeconds(60 * 60 * 4); // Add 4 hours + dueDate = dueDate+''; + var hours = Number((dueDate + '').slice(11, 13)); + + if (hours >= 0 && hours < 12) { + gs.addInfoMessage('if Inside 8-5/7'); + dueDateVal = dueDate.slice(0, 10) + " 17:00:00"; + return dueDateVal; + + } else if (hours >= 17 && hours <= 23) { + var nextDate = new GlideDateTime(created); + nextDate.addDaysUTC(1); + var newDue = new GlideDateTime(nextDate.getDate().getValue() + " 17:00:00"); + dueDateVal = newDue; + return dueDateVal; + } else { + dueDateVal = dueDate; + return dueDateVal; + } + + } + + // 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 + else if (priority == 3 || priority == 4) { + var schedule = new GlideSchedule(); + // cmn_schedule for the Holidays + var scheduleId = 'bd6d74b2c3fc72104f7371edd40131b7'; + schedule.load(scheduleId); + + var nextDay = new GlideDateTime(created); + nextDay.addDaysUTC(1); + + + //Checking for weekends + var dayOfWeek = nextDay.getDayOfWeekUTC(); + + var isWeekend = (dayOfWeek == 6 || dayOfWeek == 7); + + + // Loop until next working day (weekdays excluding holidays) + while (schedule.isInSchedule(nextDay) || isWeekend) { + nextDay.addDaysUTC(1); + dayOfWeek = nextDay.getDayOfWeekUTC(); + isWeekend = (dayOfWeek == 6 || dayOfWeek == 7); + } + + // Set to 12:00 PM on that valid day + var validDate = new GlideDateTime(nextDay.getDate().getValue() + " 17:00:00"); + return validDate; + } + }, + + type: 'CalculateDueDates' +}; \ No newline at end of file diff --git a/Client-Side Components/UI Actions/Populate Due Date based on Priority/UI Action.js b/Client-Side Components/UI Actions/Populate Due Date based on Priority/UI Action.js new file mode 100644 index 0000000000..2597aea189 --- /dev/null +++ b/Client-Side Components/UI Actions/Populate Due Date based on Priority/UI Action.js @@ -0,0 +1,34 @@ +/* +Table - incident +Show Update - True +Form Button - True +Condition - (current.due_date == '' && current.priority != '5') + +Input +1. Created Date +2. Priority + +Validation +Will not appeare if the value is already there and the priority is 5 + +Output +1. Due Date + +*/ + + +// 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. +function duedate() { + + var priority = current.getValue('priority'); + var created = current.getDisplayValue('sys_created_on'); + var si = new CalculateDueDates(); + var response = si.findDueDate(priority, created); + var gdt = new GlideDateTime(); + gdt.setDisplayValue(response); + current.setValue('due_date', gdt); + current.update(); + action.setRedirectURL(current); + +} +duedate(); \ No newline at end of file