Calculate business days and set due date

Chenab Khanna
Tera Expert

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 !

1 ACCEPTED SOLUTION

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;

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@Chenab Khanna 

If it's working fine for 5 then it should work fine for 30 days as well

What date is expected and what date is being printed in logs?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Its giving 3 days before for 30 and 5-6 days before for 60.

@Chenab Khanna 

how is the schedule defined?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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;