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

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

Hi Ankur,

Based on CSI combinations the SLA days gets populated if the created date is 24/10/19(Thursday) and SLA Days(1 Days(s)) i need the due date to be 25/10/19(Friday). But the i seeing it to be(28/10/19) i.e. Monday.

REF::

find_real_file.png

@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

Hello Ankur,

I have a similar situation where a business rule does a rolling date from a specific start date. The future dates should exclude weekends.

(function executeRule(current, previous /*null when async*/) {

var loop_max = current.u_service_request_count;//occurence field must be integer

var end_date = current.u_service_end_date;	
	
var rollingDate = new GlideDateTime(current.u_last_service_review);

	for (var i = 0; i <= loop_max; i++) {

       createENG();

}

function createENG() {
	
var engagement = new GlideRecord('interaction');

		engagement.assigned_to = current.u_te_primary.getDisplayValue();	
		rollingDate.addMonthsUTC(3); //Add 3 months to rolling date
		engagement.setValue('u_due_date', rollingDate);
	
		if (rollingDate < end_date)
		
		engagement.insert();
	
}

})(current, previous);

 

I think I need to put this somewhere, but not sure:

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

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

 

Best regards,

Desmo