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.

Need to get backlog tickets(Incidents) older than 30 business days(excluding Holidays and weekends)

aish1234
Tera Contributor
 
1 ACCEPTED SOLUTION

I got the result by below script.

//Client callable script include

var BusinessDays = Class.create();
BusinessDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    Bdays: function() {
       
        var temp = new ExcludeHolidays().holidays();  // getting last business day (considering 30 business  days) refer script below.
        var array = [];
        var c = 0;
        var gr = new GlideRecord('incident');
        gr.addEncodedQuery('sys_created_on<' + temp);//assignment group Parent is AAFR_XX_CG_ComputaCenter
        gr.query();
        while (gr.next()) {
            array.push(gr.number.toString());
        }

        return array;

    },
type: 'BusinessDays'
});
 
 ============//getting last business day from today(30 days ago)
var ExcludeHolidays = Class.create();
ExcludeHolidays.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    holidays: function() {
        var count = 0;
        var tday = new GlideDateTime();
        var temp = tday;  //temporary variable

        while (count < 30) {

            if (temp.getDayOfWeekUTC() != 6 && temp.getDayOfWeekUTC() != 7) {
                count = count + 1;
            } //excluding weekends

            var schedule = "US Holidays";
            var sch = new GlideRecord("cmn_schedule");
            sch.addQuery("name", schedule);
            sch.query();
            if (sch.isInSchedule(temp)) {

                count = count - 1;  //reducing count since last count was including holidays
            }

            temp.addDaysUTC(-1);  // reducing day by 1

        }
        return temp; // returning business day 30 days ago 
    },
 
 
//in the filter you need to add
Number is one of javascript&colon; new BusinessDays().Bdays();
 
 

View solution in original post

6 REPLIES 6

You do have schedules? Since they are mainly used within SLA management, it seems like you are just not handling your processes correctly and now need a solution for that. 

Easiest way: create an sla definition (since you aren't using them, it doesn't matter what you put in it. Start time on opened, stop time on closed would be a good start. And then run a 'repair sla' on all your open incidents. It will give you the data you need in a much faster way than creating a script to validate on a schedule that may not even exist to get these records.

Following the SLA definition solution will give you a very fast and up to date, reportable view of the data.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

I got the result by below script.

//Client callable script include

var BusinessDays = Class.create();
BusinessDays.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    Bdays: function() {
       
        var temp = new ExcludeHolidays().holidays();  // getting last business day (considering 30 business  days) refer script below.
        var array = [];
        var c = 0;
        var gr = new GlideRecord('incident');
        gr.addEncodedQuery('sys_created_on<' + temp);//assignment group Parent is AAFR_XX_CG_ComputaCenter
        gr.query();
        while (gr.next()) {
            array.push(gr.number.toString());
        }

        return array;

    },
type: 'BusinessDays'
});
 
 ============//getting last business day from today(30 days ago)
var ExcludeHolidays = Class.create();
ExcludeHolidays.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    holidays: function() {
        var count = 0;
        var tday = new GlideDateTime();
        var temp = tday;  //temporary variable

        while (count < 30) {

            if (temp.getDayOfWeekUTC() != 6 && temp.getDayOfWeekUTC() != 7) {
                count = count + 1;
            } //excluding weekends

            var schedule = "US Holidays";
            var sch = new GlideRecord("cmn_schedule");
            sch.addQuery("name", schedule);
            sch.query();
            if (sch.isInSchedule(temp)) {

                count = count - 1;  //reducing count since last count was including holidays
            }

            temp.addDaysUTC(-1);  // reducing day by 1

        }
        return temp; // returning business day 30 days ago 
    },
 
 
//in the filter you need to add
Number is one of javascript&colon; new BusinessDays().Bdays();