Scheduled JOB Script for the Change Request

GBS
Tera Contributor

Hi, I'm using the script below, which is not working. Can anyone help me with the script to trigger the notification for the below
A notification has to be triggered 1 workday after the planned end date of a change is reached, and the Change Type is Normal, and the State is Implement

(function executeRule(current, gsr) {
    var gr = new GlideRecord('change_request');
    gr.addQuery('type', 'normal'); // Change Type = Normal
    gr.addQuery('state', 'implement'); // State = Implement

    // Planned end date is 1 working day ago
    var gdt = new GlideDateTime();
    gdt.subtractDays(1);
    // Skip weekends
    while (isWeekend(gdt)) {
        gdt.subtractDays(1);
    }

    gr.addQuery('end_date', '<=', gdt);
    gr.query();

    while (gr.next()) {
        gs.eventQueue('change.implement.overdue', gr, gr.sys_id, gs.getUserID());
    }

    function isWeekend(dateTime) {
        var day = parseInt(dateTime.getDayOfWeekLocalTime(), 10); // 1 = Monday, 7 = Sunday
        return day === 6 || day === 7;
    }
})(current, gsr);

 

9 REPLIES 9

Can you tell me what is happening? Is it adding all the Change requests or not ignoring week ends?

Thank you,
Palani

Ankur Bawiskar
Tera Patron
Tera Patron

@GBS 

where are you writing this script?

You can use a daily scheduled job

1) iterate if planned end data is reached/crossed

2) add 1 business day to it

3) then use gs.eventQueueScheduled() to trigger the event on that date/time

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar Hi Ankur, I'm using the daily scheduled job with the script. Can you please share the script 

@GBS 

shared below

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@GBS 

your scheduled job script should be something like this but please enhance

I am sending email to assigned to user

// Scheduled Script Execution: Notify 1 Workday After Planned End Date

var gr = new GlideRecord('change_request');
gr.addQuery('type', 'normal'); // Change Type = Normal
gr.addQuery('state', 'implement'); // State = Implement
gr.addNotNullQuery('end_date'); // Planned end date is set
gr.query();

while (gr.next()) {
    var plannedEnd = new GlideDateTime(gr.end_date);
    var workdaysAdded = 0;

    // Add 1 workday to planned end date, skipping weekends
    while (workdaysAdded < 1) {
        plannedEnd.addDaysLocalTime(1);
        var day = parseInt(plannedEnd.getDayOfWeekLocalTime(), 10); // 1 = Monday, 7 = Sunday
        if (day >= 1 && day <= 5) {
            workdaysAdded++;
        }
    }

    // If today is equal to or after (>=) the calculated date, trigger the notification
    var now = new GlideDateTime();
    if (now >= plannedEnd) {
        gs.eventQueue('change.implement.overdue', gr, gr.sys_id, gr.assigned_to.email.toString());
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader