Scheduled Job in ServiceNow for approval reminder notifications

JyotiranjanC566
Tera Contributor

Hi Team,

I have created a Scheduled Job in ServiceNow for approval reminder notifications. The requirement is that the job should run only on Riyadh business working days (Sunday to Thursday) and should not execute on Friday and Saturday.

However, currently the Scheduled Job is running daily including weekends.

Current requirement:

  • Run only Sunday to Thursday
  • Timezone should follow Riyadh timezone
  • Skip Friday and Saturday completely

I tried using schedule conditions, but the job is still executing daily.

Could anyone please suggest the best approach to Solve it.

 

Current script logic example:

// ================= DAY CHECK =================
var gdtToday = new GlideDateTime();
gdtToday.setStartOfDay();

var day = parseInt(gdtToday.getDayOfWeekLocalTime(), 10);

// Run only Sunday–Thursday
if (day != 5 && day != 6) {

    // ================= CALCULATE 10 BUSINESS DAYS =================
    var count = 0;
    var pastDate = new GlideDateTime(gdtToday);

    while (count < 10) {

        pastDate.addDaysLocalTime(-1);

        var d = parseInt(pastDate.getDayOfWeekLocalTime(), 10);

        // Skip Friday (5) and Saturday (6)
        if (d != 5 && d != 6) {
            count++;
        }
    }

    // VERY IMPORTANT → include full day
    pastDate.setDisplayValue(pastDate.getLocalDate() + " 23:59:59");

    gs.info("Auto-close running. Cutoff date: " + pastDate.getDisplayValue());

    // ================= INCIDENT =================
    var appr = new GlideRecord('incident');
    appr.addQuery('active', true);
    appr.addQuery('state', 3);
    appr.addQuery('sys_updated_on', '<=', pastDate);
    appr.addQuery('hold_reason', '6');
    appr.query();

    var incidentCount = 0;

    while (appr.next()) {

        gs.eventQueue('autocancel.incident', appr, "", "");

        appr.state = '8';
        appr.sys_updated_by = 'system';
        appr.comments = "This ticket has been automatically closed due to no activity over the past 10 business days (Sun–Thu). On Hold Reason: " + appr.getDisplayValue('hold_reason');

        appr.update();
        incidentCount++;
    }

    // ================= RITM =================
    var gr_SRonhold = new GlideRecord('sc_req_item');
    gr_SRonhold.addQuery('active', true);
    gr_SRonhold.addQuery('state', 17);
    gr_SRonhold.addQuery('sys_updated_on', '<=', pastDate);
    gr_SRonhold.addQuery('u_on_hold_reason', '2');
    gr_SRonhold.query();

    var ritmCount = 0;

    while (gr_SRonhold.next()) {

        var ritmSysId = gr_SRonhold.sys_id;

        // Reject approvals
        var notapproved = new GlideRecord('sysapproval_approver');
        notapproved.addQuery('source_table', 'sc_req_item');
        notapproved.addQuery('sysapproval', ritmSysId);
        notapproved.addQuery('state', 'requested');
        notapproved.query();

        while (notapproved.next()) {
            notapproved.state = 'rejected';
            notapproved.comments = "Auto-closed after 10 business days. On Hold Reason: " + gr_SRonhold.getDisplayValue('u_on_hold_reason');
            notapproved.update();
        }

        // Close RITM
        gr_SRonhold.state = '4';
        gr_SRonhold.comments = "This ticket has been automatically closed due to no activity over the past 10 business days. On Hold Reason: " + gr_SRonhold.getDisplayValue('u_on_hold_reason');
        gr_SRonhold.update();

        // Update Request
        var grReq = new GlideRecord('sc_request');
        grReq.addQuery('sys_id', gr_SRonhold.request);
        grReq.query();

        while (grReq.next()) {
            grReq.state = '4';
            grReq.update();
        }

        // Update Catalog Tasks
        var grtask = new GlideRecord('sc_task');
        grtask.addQuery('request_item', ritmSysId);
        grtask.query();

        while (grtask.next()) {
            grtask.state = '4';
            grtask.update();
        }

        gs.eventQueue('autocancel.requestItem', gr_SRonhold, "", "");
        ritmCount++;
    }

    // ================= LOG =================
    gs.info("Auto-close completed. Incidents: " + incidentCount + ", RITMs: " + ritmCount);

} else {

    gs.info("Auto-close job skipped (Weekend)");

}
1 REPLY 1

Tanushree Maiti
Tera Patron

Hi @JyotiranjanC566 

 

Try with this condition. Update your code logic in Run script field. ,execute and check.

 

answer = checkCondition();

function checkCondition(){

    var dayOfWeek = new GlideDateTime().getDate().getDayOfWeek();

    // Monday is 1 and there onwards

    if(dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 4 ||  dayOfWeek == 7)

        return true;

    else

        return false;  

}

 

Note: If you check It is not checking Riyadh Timezone, try with

answer = checkCondition();

function checkCondition(){

    var dayOfWeek = new GlideDateTime().getDayOfWeekLocalTime();

    // Monday is 1 and there onwards

    if(dayOfWeek == 1 || dayOfWeek == 2 || dayOfWeek == 3 || dayOfWeek == 4 ||  dayOfWeek == 7)

        return true;

    else

        return false;  

}

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti