
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2023 03:54 AM - edited 04-27-2023 03:56 AM
Hi,
I have a requirement to set due date field in a form by calculating 5/30/60 business days from current date.
I have written a before update BR as below -
(function executeRule(current, previous /*null when async*/ ) {
// if state is draft set due date value 5 days later from current date
// if state is planning set due date value 30 days later from current date
// if state is open set due date value 30 days later from current date
var getSchedule = gs.getProperty('sn_grc.eps_scheduler_businessWorkingDays');
var schedule = new GlideSchedule(getSchedule);
var dur = '';
var end = '';
try {
if (current.state == '1') { // 1 = draft
var dueDateDraft = new GlideDateTime();
var daysDraft = 5; //multiply days with working hours = 8
dur = new GlideDuration(60 * 60 * 1000 * daysDraft * 8);
end = schedule.add(dueDateDraft, dur);
current.due_date = end;
} else if (current.state == '2') { // 2 = planning
var dueDatePlanning = new GlideDateTime(current.work_start);
var daysPlanning = 30; //multiply days with working hours = 8
dur = new GlideDuration(60 * 60 * 1000 * daysPlanning * 8);
end = schedule.add(dueDatePlanning, dur);
current.due_date = end;
} else if (current.state == '5') { // 5 = open
var dueDateOpen = new GlideDateTime();
var daysOpen = 60; //multiply days with working hours = 8
dur = new GlideDuration(60 * 60 * 1000 * daysOpen * 8);
end = schedule.add(dueDateOpen, dur);
current.due_date = end;
} else if (current.state.changesFrom(5) && current.state.changesTo(0)) { // 5 = planning, 0 = closure review
current.due_date = '';
}
} catch (ex) {
gs.info("Error in BR: Set Due Date Based on State - " + ex);
}
})(current, previous);
The code works correctly for 5 days but gives incorrect results for 30 and 60. Is there any other way to do so or is there any mistake that needs correction?
Any help would be appreciated. Thanks in advance !
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2023 04:49 AM
Got the issue, the working hours are configured as 9 and i was using 8. Corrected that here -
var getSchedule = gs.getProperty('sn_grc.eps_scheduler_businessWorkingDays');
var schedule = new GlideSchedule(getSchedule);
var dur = '';
var end = '';
try {
if (current.state == '1') { // 1 = draft
var dueDateDraft = new GlideDateTime();
var daysDraft = 5; //multiply days with working hours = 9
dur = new GlideDuration(60 * 60 * 1000 * daysDraft * 9);
end = schedule.add(dueDateDraft, dur);
current.due_date = end;
} else if (current.state == '2') { // 2 = planning
var dueDatePlanning = new GlideDateTime();
var daysPlanning = 30; //multiply days with working hours = 9
dur = new GlideDuration(60 * 60 * 1000 * daysPlanning * 9);
end = schedule.add(dueDatePlanning, dur);
current.due_date = end;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2024 08:01 PM
Could you please help me with the below quey
Re: How to subtract days from a schedule - ServiceNow Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2023 05:02 AM
// Define the number of business days to add
var businessDays = [5, 30, 60];
// Calculate the due date for each business day value and set the field value
for (var i = 0; i < businessDays.length; i++) {
var dueDate = new GlideDateTime();
var currentDay = dueDate.getDayOfWeek();
// Check if the current day is Saturday or Sunday, and adjust accordingly
if (currentDay == 6) {
dueDate.addDays(2);
} else if (currentDay == 0) {
dueDate.addDays(1);
}
// Add the specified number of business days to the current date
for (var j = 0; j < businessDays[i]; j++) {
dueDate.addDays(1);
while (dueDate.getDayOfWeek() == 6 || dueDate.getDayOfWeek() == 0) {
dueDate.addDays(1);
}
}
// Set the field value for the current business day
g_form.setValue('due_date_' + businessDays[i], dueDate);
}
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/