Scripting Help

DEEPAK KUMAR SI
Tera Contributor

Hi All,

I need help in scripting. I have String field where I have to populate the count of days past since INC was created excluding the weekends (i.e sat & sun). I am able to achieve the count of days but need help in excluding the weekends.

--------------------------------

Using below script in Calculate value of the field.

 

(function calculatedFieldValue(current) {


var startDate = new GlideDateTime(current.getValue('sys_created_on'));
var endDate = new GlideDateTime();
var dayCount = GlideDateTime.subtract(startDate,endDate);

return dayCount.getDayPart();

})(current);

1 ACCEPTED SOLUTION

SN_Learn
Kilo Patron
Kilo Patron

Hi  @DEEPAK KUMAR SI ,

 

Please try the below, tested in PDI:

(function calculatedFieldValue(current) {

    var startDate = new GlideDateTime(current.getValue('sys_created_on'));
    var endDate = new GlideDateTime();
    var diffDays = 0;
    while (startDate.compareTo(endDate) < 0) {
        startDate.addDays(1);
        var weekDay = startDate.getDayOfWeek();

        if (weekDay != 6 && weekDay != 7) {
            diffDays++;
        }
    }

    return diffDays;

})(current);

 

Output:

SN_Learn_0-1719145978729.png

 

 

Mark this as Helpful / Accept the Solution if this helps

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

3 REPLIES 3

Omkar Kumbhar
Mega Sage
Mega Sage

Hello @DEEPAK KUMAR SI ,

Please use the GlideSchedule API for this use case.

Please find the below link which will help you to script it.

https://www.servicenow.com/community/developer-forum/getting-number-of-working-days-between-start-da...

 

Thank you,

Omkar

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

Using this schedule

DEEPAKKUMARSI_0-1719141755039.png

and below is script.. now i am getting 0 as value in the field.

 

(function calculatedFieldValue(current) {

// Add your code here
var sd = new GlideDateTime(current.getValue('sys_created_on'));
var ed = new GlideDateTime();
var schedule = new GlideSchedule();
schedule.load('d2fcb99887074e10b2a165380cbb352f');
var dur = schedule.duration(sd,ed);

return dur.getDayPart();


})(current);

SN_Learn
Kilo Patron
Kilo Patron

Hi  @DEEPAK KUMAR SI ,

 

Please try the below, tested in PDI:

(function calculatedFieldValue(current) {

    var startDate = new GlideDateTime(current.getValue('sys_created_on'));
    var endDate = new GlideDateTime();
    var diffDays = 0;
    while (startDate.compareTo(endDate) < 0) {
        startDate.addDays(1);
        var weekDay = startDate.getDayOfWeek();

        if (weekDay != 6 && weekDay != 7) {
            diffDays++;
        }
    }

    return diffDays;

})(current);

 

Output:

SN_Learn_0-1719145978729.png

 

 

Mark this as Helpful / Accept the Solution if this helps

 

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.