I need to set due date based on working days(5 working days) and skip weekends this has to be done based on created date

Hari1
Mega Sage

Hi,

I need to set due date based on working days only(5 working days)

If it's saturday(6) or sunday(7) i need to move the date to monday. I mean i need to skip the weekends and count only the working days to resolve an incident to set the due date.

I have written a business rule:

REF::

var days = 0 ;
var createdDate = new GlideDateTime(current.sys_created_on);
gs.log("createdDate is: " + createdDate);

var getSlaDays = current.u_sla_days;
gs.log("getSlaDays is: " + getSlaDays);

var dueDate;
var splitSlaDays = getSlaDays.split(" ");
gs.log("splitSlaDays is: " + splitSlaDays);

var daysValue = splitSlaDays[0];
gs.log("daysValue is: " + daysValue);

//dueDate = createdDate + daysValue;
//dueDate = createdDate.addDaysUTC(daysValue);
if(createdDate.getDayOfWeek() != 6 && createdDate.getDayOfWeek() != 7)
{ //exclude weekends
days++;
gs.log("days is: " + days);
}
createdDate.addDays(1) ;
createdDate.addDaysUTC(daysValue);
gs.log("createdDate is: " + createdDate);


dueDate = createdDate + days;
gs.log("dueDate increment is: " + dueDate);
if( dueDate.getDayOfWeek() == 6)
{
dueDate = dueDate + 2 ;
gs.log("Inside if 6: " + dueDate);
}
if ( dueDate.getDayOfWeek() == 7)
{
dueDate = dueDate + 1 ;
gs.log("Inside if 7: " + dueDate);
}
gs.log("current.u_due_date is : " + dueDate);
current.u_due_date = dueDate;

If the sladays is 2 days to resolve a ticket and the ticket is created on 24/10/19 i need to get the output as 28/10/19 to be set on the due date.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Hemanth,

So after days from daysValue to the created date if the day is saturday or weekend then get the coming monday as the date?

use below script

var createdDate = new GlideDateTime(current.sys_created_on);
gs.log("createdDate is: " + createdDate);

var getSlaDays = current.u_sla_days;
gs.log("getSlaDays is: " + getSlaDays);

var dueDate;
var splitSlaDays = getSlaDays.split(" ");
gs.log("splitSlaDays is: " + splitSlaDays);

var daysValue = splitSlaDays[0];
gs.log("daysValue is: " + daysValue);

// add days to created date

createdDate.addDaysUTC(daysValue);

// if after adding days it comes as saturday i.e. day as 6 so add 2 days so that it comes to Monday
// if after adding days it comes as sunday i.e. day as 7 add 1 day so that it comes to Monday

if(createdDate.getDayOfWeekUTC() == 6)
createdDate.addDaysLocalTime(2);

else if(createdDate.getDayOfWeekUTC() == 7)
createdDate.addDaysLocalTime(1);

current.u_due_date = createdDate;

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

8 REPLIES 8

@Hemanth 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

 

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

@Hemanth 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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

Community Alums
Not applicable

I'd tackle this in flow designer by adding a relative duration, as you can attach a schedule to make sure you're targetting only working days. 

In the original solution you've posed, it's not going to take into account any public holidays etc.

Jack
Tera Guru

Hi,

If there is any requirement related to working day/hours/bank holiday, I think you should consider to use schedule to calculate.

GlideSchedule & GlideDuration API.