- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2020 05:44 AM
I have another item where I have set the due date to 10 business days from the opened date of the item, however I have a little more tricky one. I need to take the date/time from a variable(date_time_deployment) the user selects and add 3 business days and 4 hours to it for the due date. I put together this script to get the 3 days as a start but it's not working correctly. Any ideas on how to get this accomplished? I'm using a business rule to accomplish this.
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-8 weekdays excluding holidays');
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//Get the current date/time in correct format for duration calculation
var currentDateTime = current.variables.date_time_deployment;
//Set the amount of time to add (in seconds)
var timeToAdd = 3 * 43200;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');
//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2020 01:22 PM
If its a business rule on Requested Item [sc_req_item] table then
current.variables.date_time_deployment
should give you the value.
I tested and this code should work for you.
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-8 weekdays excluding holidays');
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//Get the current date/time in correct format for duration calculation
var currentDateTime = new GlideDateTime();
currentDateTime.setDisplayValue(current.variables.date_time_deployment);
//Set the amount of time to add (in seconds)
var timeToAdd = 3 * 43200 + 4 * 3600;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');
//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;
In case you are writing this BR in after or async then dont miss to add this line at the end of above script.
current.update();
-Tanaji
Please mark response correct/helpful if applicable

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2020 06:09 AM
Hi,
Please go through the script for 3 working days.
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-5 weekdays');
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//Get the current date/time in correct format for duration calculation
var currentDateTime = current.opened_at.getGlideObject();
//Set the amount of time to add (in seconds)
var timeToAdd = 8 * 32400;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');
//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;
Ali Shaikh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2020 06:53 AM
Thank you, but I've tried this and replaced the schedule with our 8-8 schedule... i've also replaced the opened_at variable with our variable. It's not working correctly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2020 09:50 AM
The code below is working as far as the calculations go, however it's using the opened_by date. As soon as I change the script to look at the variable date_time_deployment it not longer works and the Service Portal throws a quick exception error and submits but I can't read it all before it goes away.
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-8 weekdays excluding holidays');
if (typeof GlideSchedule != 'undefined')
var sched = new GlideSchedule(schedRec.sys_id);
else
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//Get the current date/time in correct format for duration calculation
var currentDateTime = current.opened_at.getGlideObject();
//Set the amount of time to add (in seconds)
var timeToAdd = 3 * 43200 + 4 * 3600;
durToAdd = new GlideDuration(timeToAdd*1000);
var newDateTime = sched.add(currentDateTime, durToAdd, '');
//Set the 'due_date' field to the new date/time
current.due_date = newDateTime;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2020 09:58 AM
May I ask where you are writing this script?
-Tanaji
Please mark reply correct/helpful if applicable