Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to skip the weekends and consider only weekdays to set due date

Hari1
Mega Sage

Hi,

I am setting due date based in SLA days and created date i.e. if i am creating a ticket the due date has to be set dynamically based on the SLA days and created date.

I am able to get the due date with the help of client script and script include. But it's counting the weekends as well. I need the due date to exclude weekends and take only the weekdays(5 working days (Monday to Friday)) into count.

find_real_file.png

REF:: Below is the script

Script include:

MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor,
{ 
	nowDateTime: function ()
{
		gs.log("MyDateTimeAjax");
		var createdDate = new GlideDateTime(gs.nowDateTime());
		var nd = new GlideDateTime(gs.nowDateTime());
		var dueDateValue = "";
		var days = 0 ;
		
		gs.log("nd value is" + nd);
		
		var cat = this.getParameter('sysparm_cat');
		var subcat = this.getParameter('sysparm_subcat');
		var itm = this.getParameter('sysparm_itm');
		
		var gr = new GlideRecord("u_sla_admin_details");
		gr.addQuery("u_category", cat);
		gr.addQuery("u_sub_category", subcat);
		gr.addQuery("u_item", itm);
		gr.addQuery("u_record_type", "ADMIN");
		gr.addQuery("u_active", "true");
		gr.query();
		
		if(gr.next()) 	
		{ 	
		var slaResult = (gr.u_ttr * 1 );
		var slaCall = "";
		
		if (gr.u_sla_days != "")
		{ 
			slaCall = (gr.u_sla_days * 1);
           
			gs.log("value of days before increment is " + days);
			
			if(nd.getDayOfWeekUTC() == 6)
			{
				nd.addDaysLocalTime(2);
			}
			else if(nd.getDayOfWeekUTC() == 7)
			{
				nd.addDaysLocalTime(1);
			}
			gs.log("nd value before incrementing us: " + nd);
			nd.addDaysUTC(slaCall);
			gs.log("Value of nd after incrementing us:  " + nd);
			while (createdDate < nd)
				{
				if (createdDate.getDayOfWeekUTC() != 6 && createdDate.getDayOfWeekUTC() != 7)           
				//excluding Weekends
				gs.log("value of createdDate if not 6 and 7 is: " + createdDate);
				  {
				  gs.log("value of days before increment is " + days);
					  days++ ;
				  gs.log("value of days after increment is " + days);
				  }
					createdDate.addDaysLocalTime(1);
					gs.log("createdDate Value is: " + createdDate);
				}
			gs.log("createdDate Value completed is: " + createdDate);
			gs.log("nd Value completed is: " + nd);
			
			if (createdDate.getDayOfWeekUTC() == nd.getDayOfWeekUTC()) 
			{
			gs.log("createdDate Value after processing: " + createdDate);	
			return createdDate;
			}
				
			else if(createdDate.getDayOfWeekUTC() == 6 )
				{
				createdDate.addDaysLocalTime(2);
					return createdDate;
				}
				else if(createdDate.getDayOfWeekUTC() == 7)
				{
				createdDate.addDaysLocalTime(1);
					return createdDate;
				}
			//var duedate = nd;
			//gs.log("createdDate value is: " + createdDate);
			//gs.log("duedate value is: " + duedate);
			 /*while (createdDate < duedate)
				{
				if (createdDate.getDayOfWeek() != 6 && createdDate.getDayOfWeek() != 7)           
				//excluding Weekends
				
				  {
				  gs.log("value of days before increment is " + days);
					  days++ ;
				  gs.log("value of days after increment is " + days);
				  }
					createdDate.addDays(1);
				}*/
			
			//gs.log("Days value is: " + duedate);
			//return duedate;
			//return nd;
		}
		else
			{
				slaCall = "Not Applicable";
				nd = slaCall;
				gs.log("Inside MyDateTimeAjax else part nd value: " + nd);
				return nd;
            }
		
		}
}
});

 

3 REPLIES 3

VigneshMC
Mega Sage

You can use schedule to make this easier, create an schedule which runs 24*5 weekdays and use it your script

Your schedule should look like this

find_real_file.png

find_real_file.png

Then use this schedule in your script

var startDate = new GlideDateTime();
var days = 10;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days); // your duration goes here
var schedule = new GlideSchedule('e1cc05f1db7440104c22e1bb4b96192f'); //schedule sys_id
var end = schedule.add(startDate, dur);
gs.info(startDate.getDisplayValue());
gs.info(end.getDisplayValue());

Dave_H
Tera Contributor

I had a similar requirement. This works perfectly. Thank you!

SatheeshKumar
Kilo Sage

you can use the Duration calculation to calculate due date by skipping required days.

 

check the below link for details :

https://docs.servicenow.com/bundle/newyork-application-development/page/script/server-scripting/conc...

 

 

-Satheesh