- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2017 06:28 AM
I can't seem to figure out how to add a schedule to a script that calculates a custom targeted due date field based on a requested_date variable. I would like the script to take todays date and add 3 days excluding weekends. Does anyone have this script already?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-18-2017 12:30 PM
Hi Danielle,
Strange, I created a test Catalog Item with a Date/Time variable and used the script, but changed line 22 to just:
dueDateGdt;
It seemed to work for me to set the date for 3 days.
Screenshot of Date/Time variable:
Screenshot of Default Value of Date/Time variable:
Default Value code:
javascript: var nowGdt = new GlideDateTime(); var nowGdt = new GlideDateTime(); var myScheduleName = '8-5 weekdays'; var dueDays = 3; var dueWorkingHours = 8; var dueSeconds = dueDays*dueWorkingHours*60*60; var leadTime = new GlideDuration(dueSeconds*1000); 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, '');} dueDateGdt;
Thanks,
John
P.S. Normally, we would create a custom Script Include with a helper function: getDueDate('schedule_name', 'number_of_days') and then use that for the variable default value: javascript:new myCatalogVariableHelper().getDueDate('8-5 Weekday', 3);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2017 05:30 PM
Hi Danielle,
You might need to change:
if (schedRec.get('8-5 weekdays', myScheduleName))
to
if(schedRec.get('name', myScheduledName))
For reference: GlideRecord - ServiceNow Wiki
Thanks,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 06:16 AM
Thanks John. I updated the script that is being used in the default value of the variable and I'm still receiving the same results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 12:49 PM
Hi Danielle,
You might also need to change the calculation of leadTime to use "dueSeconds":
var leadTime = new GlideDuration(dueSeconds*1000);
When I tried this in my instance:
// Get the datetime now
var nowGdt = new GlideDateTime();
// Get the datetime now
var nowGdt = new GlideDateTime();
// The name of the schedule
var myScheduleName = '8-5 weekdays';
// The basis of our calculation
var dueDays = 3;
var dueWorkingHours = 8;
var dueSeconds = dueDays*dueWorkingHours*60*60;
var leadTime = new GlideDuration(dueSeconds*1000);
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("Result=" + dueDateGdt);
It seemed to return the correct date: *** Script: Result=2017-04-20 16:47:46
Thanks,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2017 01:19 PM
Interesting, I tried the updated script on your environment as the variables default value and it is still only posting the current time/date. I don't have any other scripts or business rules running on this catalog item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-25-2017 12:16 PM
This works perfectly in a business rule. Thanks for sharing!