
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2018 07:48 AM
I'm trying to get several dates based off of catalog variables either 1 day ahead, 4 days in the past, and 5 days in the past based off the M-F no holidays schedule.
As of now, the duration is not working to subtract the days and the date is logging as the gdt date.
Any help would be appreciated.
//Get Date & M-F no weekends schedule
var gdt = new GlideDateTime(current.variables.audit_start_date);
var sched = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
//1 Day Delivery Time
var days1 = 1;
var dur1 = new GlideDuration(60 * 60 * 24 * 1000 * days1);
var end1 = sched.add(gdt,dur1);
workflow.scratchpad.end1 = end1;
//4 Day Delivery Time
var days4 = -4;
var dur4 = new GlideDuration(60 * 60 * 24 * 1000 * days4);
var end4 = sched.add(gdt,dur4);
workflow.scratchpad.end4 = end4;
gs.log(end4);
gs.log("audit_start"+gdt);
//5 Day Delivery Time
var days5 = -5;
var dur5 = new GlideDuration(60 * 60 * 24 * 1000 * days5);
var end5 = sched.add(gdt,dur5);
workflow.scratchpad.end5 = end5;
//Set Task Due Date to 4 days prior to onboarding of new Auditor unless the audit start date is >= than current date. To modify access, task due dates are set for the next business day.
var task = new GlideRecord ('sc_task');
if (current.variables.access_type == 'new_access'){
if(gdt > gs.NOW.getDisplayValue()){
task.due_date = workflow.scratchpad.end1;
gs.log("DAY1");
gs.log(gs.now.getDisplayValue());
}
task.due_date = workflow.scratchpad.end4;//Setting the Task Due Date
gs.log("DAY4");
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2018 01:53 PM
I looked at this a bit and I think it's a bit difficult to give you specific code without more details, so I think I'll just provide a couple of my thoughts that came out of that and an example of where you want to go:
- It's best if your user is providing you a date and not a date/time - this will allow you to disregard their "time" for your calculations
- I think the logic might need to be adjusted: You're saying if the start date is in the future, the due date is four days prior to that. So, if my start date is in two days, my due date is two days ago and I'm already overdue to deliver?
- I think your main concern is that your due date is being set on a working day, which is where you want the schedule to come into play.
This code below is incomplete, but it should start getting you on the right track. For example, in my test ticket, i made the start date 6/18, which put my due date as 6/14 - you can see it's not taking into account business days. Not sure if you need to add extra days for delivery if part of the time is over a weekend or holiday? Also, the logic around when to make the due date 4 days before the start or 1 day from now might need tweaking as you work through some test scenarios:
//Get the schedule so you can grab the start time from it (this will be used on the due date and to normalize the "in schedule" calculations)
var schedID = '08fcd0830a0a0b2600079f56b1adb9ae';
var schedGr = getSchedule(schedID);
var schedStartTime = schedGr.start_date_time;
//Start time comes through as a string of date values plus T and then string for the time value, so we split on T so we can just get the time value
var schedStartArr = schedStartTime.split('T');
var startTime = schedStartArr[1];
//Now we split the time so we can build a time string to add to dates we want to calculate off of - this removes any possiblity that if "now" or "due date" isn't in the same time frame as your schedule that you constantly get false on checking if its in schedule
var startTimeArr = startTime.split('');
var startString = startTimeArr[0] + startTimeArr[1] + ':' + startTimeArr[2] + startTimeArr[3] + ':' + startTimeArr[4] + startTimeArr[5];
//We're going to use this general nowDate for the basis of our other date variables
var nowDate = new GlideDateTime();
//Now we find today's date with the due time of the start of your business days as defined in your schedule
var nowDateTime = new GlideDateTime(nowDate.getDate() + ' ' + startString);
gs.log('nowDateTime before finding in business hours: ' + nowDateTime + ' the start time is ' + startString,'dueDateTest');
//Here we get four days from now
var fourDaysFromNow = new GlideDateTime(nowDate.getDate() + ' ' + startString);
fourDaysFromNow.addSeconds(345600);
//Here we get the start date they gave us on the form
var startDate = new GlideDateTime(current.variables.start_date);
//Here we determine when they need the request fulfilled by as the start of business on that day
var neededBy = new GlideDateTime(startDate.getDate() + ' ' + startString);
//Now we want to start a glideschedule so we can be sure our due date is within the schedule
var checkSched = new GlideSchedule(schedID);
if(neededBy < fourDaysFromNow){
//need to deliver by tomorrow because we don't have four days
var tomorrow = new GlideDateTime(nowDate.getDate() + ' ' + startString);
var inSchedule = checkSched.isInSchedule(tomorrow);
while(inSchedule == 'false'){
tomorrow.addSeconds(86400);
inSchedule = checkSched.isInSchedule(tomorrow);
}
current.due_date = tomorrow;
}
else{
//we need to deliver four days before the start date
neededBy.addSeconds(-345600);
var nIn = checkSched.isInSchedule(neededBy);
gs.log(nIn,'dueDateTest');
while(nIn == 'false'){
neededBy.addSeconds(86400);
nIn = checkSched.isInSchedule(neededBy);
}
current.due_date = neededBy;
}
function getSchedule(schedID){
schedRec = new GlideRecord('cmn_schedule_span');
schedRec.addQuery('schedule',schedID);
schedRec.query();
if(schedRec.next()){
return schedRec;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2018 02:43 PM
From what you've provided, I think I understand your requirement is:
The user provides a date on which something is needed (current.variables.audit_start_date)
If the audit_start_date is in the future, then set the due date to one day after the audit start date (this is what I'm reading in your code).
Otherwise, set the due date to four days before the audit start date?
Is this what you are trying to achieve? Or are you trying to say: If the audit start is before today, give them just one day to fulfill the request. Otherwise, give them four days to fulfill the request. But you want the due date to be within the schedule.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2018 07:39 AM
Actually, my code is wrong.
I need if the audit start date is in the future, set the task due date 4 days prior to the start date otherwise set the task due date 1 day after the current date.
I think the problem is that I cannot subtract from the GlideDateTime when using the schedule or that the duration can't be a negative number. I'm very green at scripting so it could be my lack of knowledge as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2018 01:53 PM
I looked at this a bit and I think it's a bit difficult to give you specific code without more details, so I think I'll just provide a couple of my thoughts that came out of that and an example of where you want to go:
- It's best if your user is providing you a date and not a date/time - this will allow you to disregard their "time" for your calculations
- I think the logic might need to be adjusted: You're saying if the start date is in the future, the due date is four days prior to that. So, if my start date is in two days, my due date is two days ago and I'm already overdue to deliver?
- I think your main concern is that your due date is being set on a working day, which is where you want the schedule to come into play.
This code below is incomplete, but it should start getting you on the right track. For example, in my test ticket, i made the start date 6/18, which put my due date as 6/14 - you can see it's not taking into account business days. Not sure if you need to add extra days for delivery if part of the time is over a weekend or holiday? Also, the logic around when to make the due date 4 days before the start or 1 day from now might need tweaking as you work through some test scenarios:
//Get the schedule so you can grab the start time from it (this will be used on the due date and to normalize the "in schedule" calculations)
var schedID = '08fcd0830a0a0b2600079f56b1adb9ae';
var schedGr = getSchedule(schedID);
var schedStartTime = schedGr.start_date_time;
//Start time comes through as a string of date values plus T and then string for the time value, so we split on T so we can just get the time value
var schedStartArr = schedStartTime.split('T');
var startTime = schedStartArr[1];
//Now we split the time so we can build a time string to add to dates we want to calculate off of - this removes any possiblity that if "now" or "due date" isn't in the same time frame as your schedule that you constantly get false on checking if its in schedule
var startTimeArr = startTime.split('');
var startString = startTimeArr[0] + startTimeArr[1] + ':' + startTimeArr[2] + startTimeArr[3] + ':' + startTimeArr[4] + startTimeArr[5];
//We're going to use this general nowDate for the basis of our other date variables
var nowDate = new GlideDateTime();
//Now we find today's date with the due time of the start of your business days as defined in your schedule
var nowDateTime = new GlideDateTime(nowDate.getDate() + ' ' + startString);
gs.log('nowDateTime before finding in business hours: ' + nowDateTime + ' the start time is ' + startString,'dueDateTest');
//Here we get four days from now
var fourDaysFromNow = new GlideDateTime(nowDate.getDate() + ' ' + startString);
fourDaysFromNow.addSeconds(345600);
//Here we get the start date they gave us on the form
var startDate = new GlideDateTime(current.variables.start_date);
//Here we determine when they need the request fulfilled by as the start of business on that day
var neededBy = new GlideDateTime(startDate.getDate() + ' ' + startString);
//Now we want to start a glideschedule so we can be sure our due date is within the schedule
var checkSched = new GlideSchedule(schedID);
if(neededBy < fourDaysFromNow){
//need to deliver by tomorrow because we don't have four days
var tomorrow = new GlideDateTime(nowDate.getDate() + ' ' + startString);
var inSchedule = checkSched.isInSchedule(tomorrow);
while(inSchedule == 'false'){
tomorrow.addSeconds(86400);
inSchedule = checkSched.isInSchedule(tomorrow);
}
current.due_date = tomorrow;
}
else{
//we need to deliver four days before the start date
neededBy.addSeconds(-345600);
var nIn = checkSched.isInSchedule(neededBy);
gs.log(nIn,'dueDateTest');
while(nIn == 'false'){
neededBy.addSeconds(86400);
nIn = checkSched.isInSchedule(neededBy);
}
current.due_date = neededBy;
}
function getSchedule(schedID){
schedRec = new GlideRecord('cmn_schedule_span');
schedRec.addQuery('schedule',schedID);
schedRec.query();
if(schedRec.next()){
return schedRec;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2018 12:44 PM
Thank you so much for your time spent on this! This worked like a charm and only had to make one minor adjustment to the code to remove the single quotes from 'false' in the while loop.