- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2014 09:51 AM
I've been reviewing the Using DurationCalculator To Calculate a Due Date - ServiceNow Wiki wiki article to figure out how to set the default for a Catalog variable 'Due Date' to three business days from the 'Requested Date' = javascript:gs.now(). I think I need to create a schedule to incorporate in the scripting that is provided in the wiki article but I'm not sure how to design one that defaults to three weekdays from now. I'm still not too comfortable with scripting to but I think that is what I need, does anyone have any advice?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2014 09:46 AM
Sorry Lindsey, not having much luck here!
I've tested this and definitely got it this time...
You just need to add the below as a new line at the top of your script:
javascipt:
So it will look something like:
javascript:
// Get the datetime now
var nowGdt = new GlideDateTime();
...
Fingers crossed!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2014 10:02 AM
Hi Lindsey,
Create a new schedule that runs 9am to 5pm every week day, then use the script below to calculate the due date.
Note, you'll need to set the myScheduleName, dueDays and dueWorkingHours.
// Get the datetime now
var nowGdt = new GlideDateTime();
// The name of the schedule
var myScheduleName = 'myScheduleName';
// The basis of our calculation
var dueDays = 3;
var dueWorkingHours = 8;
// The amount of time we want for the duration
var dueSeconds = dueDays*dueWorkingHours*60*60;
var leadTime = new GlideDuration(leadTimeSeconds*1000);
// Calculate the Due Date!
var dueDateGdt;
var schedRec = new GlideRecord('cmn_schedule');
if (schedRec.get('name', myScheduleName)) {
var sched = new GlideSchedule(schedRec.sys_id);
dueDateGdt = sched.add(nowGdt, leadTime, '');
}
gs.log('The Due Date is: '+dueDateGdt.getDisplayValue()); // Hurrah!
Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2014 10:19 AM
Hi Andy,
Thanks for the scripting!! I created the '9 -5 weekdays' schedule and updated it in the scripting as you instructed me. The dueDays I left as 3 because I want it 3 business days from now and left the dueWorkingHours to 8. However, when I pull up the form after this has been entered it's populating the current date and time, instead of 3 business days from today - is there something else I forgot to change?
// Get the datetime now
var nowGdt = new GlideDateTime();
// The name of the schedule
var myScheduleName = '9 - 5 weekdays';
// The basis of our calculation
var dueDays = 3;
var dueWorkingHours = 8;
// The amount of time we want for the duration
var dueSeconds = dueDays*dueWorkingHours*60*60;
var leadTime = new GlideDuration(leadTimeSeconds*1000);
// Calculate the Due Date!
var dueDateGdt;
var schedRec = new GlideRecord('cmn_schedule');
if (schedRec.get('name', myScheduleName)) {
var sched = new GlideSchedule(schedRec.sys_id);
dueDateGdt = sched.add(nowGdt, leadTime, '');
}
gs.log('The Due Date is: '+dueDateGdt.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2014 10:06 AM
Sorry Lindsey there was a mistake in my script,
Change line:
var leadTime = new GlideDuration(leadTimeSeconds*1000);
to be:
var leadTime = new GlideDuration(dueSeconds*1000);
Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-21-2014 06:52 AM
Thanks for the correction but I still cannot get it to calculate 3 business day from today (it is still populating the current date and time). Here's a screen shot again of what I have entered in in the 'Default Value' for the Due Date variable on my form:
Could it be my schedule that's messing it up?
I am trying to trouble shoot this on my own but scripting is still very new to me, so I'm not exactly sure what I'm looking for. I appreciate your help!