How to skip the weekends while setting for 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 the script

Script include:

var MyDateTimeAjax = Class.create();
MyDateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nowDateTime: function ()
{
var getDate = new GlideDateTime(gs.nowDateTime());
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("slaCall value initially is: " + slaCall);
 if(getDate.getDayOfWeekUTC() == 6)
 { 
  gs.log("Ticket created in Saturday");
  getDate.addDaysLocalTime(2);
 }
 else if(getDate.getDayOfWeekUTC() == 7)
 {
  gs.log("Ticket created in Sunday");
  getDate.addDaysLocalTime(1);
 }

getDate.addDaysUTC(slaCall);

if(getDate.getDayOfWeekUTC() == 6 )
{
 gs.log("DayofWeek is Saturday");
 getDate.addDaysLocalTime(2);
}
if(getDate.getDayOfWeekUTC() == 7)
{
 gs.log("DayofWeek is Sunday");
 if(slaCall >= 5)
{
 gs.log("slaCall value is greater then 5: " + slaCall);
 getDate.addDaysLocalTime(2);
}
else
{
 gs.log("slaCall value is less then 5: " + slaCall);
 getDate.addDaysLocalTime(1);
}
}
return getDate;
}
else
{
slaCall = "Not Applicable";
getDate = slaCall;
return getDate;
}
}
}
});
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Hemanth,

Did you check this link?

https://community.servicenow.com/community?id=community_question&sys_id=c343bfbfdb2888544819fb243996...

I have posted an approach there

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

Hari1
Mega Sage

Hi Ankur,

Yes i did. But i am not able to exclude weekends. Please find the above screenshot. The due date should not take weekdays into count while calculating. The code that you suggested works only when the due date falls into weekends and then adds 2 for Saturday and 1 for Sunday.

I am trying to add the below code but i am not sure were to place it so that is does not take weekends

var days = 0 ;
while (nd < duedate)
{

if (nd.getDayOfWeek() != 6 && nd.getDayOfWeek() != 7)
//excluding Weekends
{
days++ ;
}
nd.addDays(1);

 

Can you please help me out with this?