- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2022 09:54 PM
Hi, hoping someone might know how to solve my calculate duration condundrum.
I looking to use the below script to calculate end dates based on a working schedule and the addition of hours based on priority. I'm just writing these to a scratchpad.
Problem I have is that the below code does not seem to be using the schedule correctly and is having an issue with timezones. Basically, I just want to write 4 end date/times to scratchpad variables for later use in the workflow. They are calculated based on current date/time.
Any point in the right direction would be appreciated.
---------------------------------------------------------------------------------------------
//Include the duration calculator and calculate due dates then write them to scratchpad
gs.include('DurationCalculator');
CalculateDueDates();
function CalculateDueDates(){
//Declare duration calculator object
var dc = new DurationCalculator();
//Add workdays schedule to the duration calculator
addSchedule(dc);
//Get current date and time
var gdt = new GlideDateTime(gs.nowNoTZ());
//Set P1 task due date + 2 working hours
dc.setStartDateTime(gdt);
dc.calcDuration(2*3600);
workflow.scratchpad.P1Due = dc.getEndDateTime();
gs.log("P1 Due - " + workflow.scratchpad.P1Due);
//Set P2 task due date + 8 working hours
dc.setStartDateTime(gdt);
dc.calcDuration(8*3600);
workflow.scratchpad.P2Due = dc.getEndDateTime();
gs.log("P2 Due - " + workflow.scratchpad.P2Due);
//Set P3 task due date = Now + 3 working days
dc.setStartDateTime(gdt);
dc.calcDuration(24*3600);
workflow.scratchpad.P3Due = dc.getEndDateTime();
gs.log("P3 Due - " + workflow.scratchpad.P3Due);
//Set P4 task due date + 10 working days
dc.setStartDateTime(gdt);
dc.calcDuration(80*3600);
workflow.scratchpad.P4Due = dc.getEndDateTime();
gs.log("P4 Due - " + workflow.scratchpad.P4Due);
}
function addSchedule(durationCalculator){
// Load the "8:30-4:30 weekdays excluding holidays" schedule into our duration calculator.
var scheduleName = "8:30-4:30 weekdays excluding holidays";
var grSched = new GlideRecord('cmn_schedule');
grSched.addQuery('name', scheduleName);
grSched.query();
if(!grSched.next()){
gs.log('*** Could not find schedule "'+ scheduleName +'"');
return;
}
durationCalculator.setSchedule(grSched.getUniqueValue(),"Australia/Queensland");
}
Solved! Go to Solution.
- Labels:
-
Request Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-13-2022 11:06 PM
Just as an FYI for those that might want to do the same. Kept playing and solved the issue. It was just timezone related - needed to user gdt.getDisplayValueInternal() as the start time value.
//Get current date and time
var gdt = new GlideDateTime();
//Set P1 task due date + 2 working hours
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(2*3600);
workflow.scratchpad.P1TaskDue = dc.getEndDateTime();
//Set P2 task due date + 8 working hours
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(8*3600);
workflow.scratchpad.P2TaskDue = dc.getEndDateTime();
//Set P3 task due date = Now + 3 working days
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(24*3600);
workflow.scratchpad.P3TaskDue = dc.getEndDateTime();
//Set P4 task due date + 10 working days
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(80*3600);
workflow.scratchpad.P4TaskDue = dc.getEndDateTime();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-13-2022 11:06 PM
Just as an FYI for those that might want to do the same. Kept playing and solved the issue. It was just timezone related - needed to user gdt.getDisplayValueInternal() as the start time value.
//Get current date and time
var gdt = new GlideDateTime();
//Set P1 task due date + 2 working hours
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(2*3600);
workflow.scratchpad.P1TaskDue = dc.getEndDateTime();
//Set P2 task due date + 8 working hours
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(8*3600);
workflow.scratchpad.P2TaskDue = dc.getEndDateTime();
//Set P3 task due date = Now + 3 working days
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(24*3600);
workflow.scratchpad.P3TaskDue = dc.getEndDateTime();
//Set P4 task due date + 10 working days
dc.setStartDateTime(gdt.getDisplayValueInternal());
dc.calcDuration(80*3600);
workflow.scratchpad.P4TaskDue = dc.getEndDateTime();