Scheduled Job that triggers event

Ricardo Sanchez
Tera Contributor
I created a scheduled job that runs daily at 7 AM. I do not need to consider the exact time the record was opened, only the date it was created. The goal is to check if the record's sys_created_on date matches today's date, then trigger an event if the record is either 10 or 13 days old.

Does this approach look correct, or is there a better way to achieve this logic?
 

(
function executeEscalationCheck() {
    var today = new GlideDateTime(gs.nowDateTime());
    today.setDisplayValue(today.getDate() + " 00:00:00");

    var gr = new GlideRecord('sc_req_item');
    gr.addEncodedQuery('active=true^cat_item=dc9db25e93f0a6108211b22efaba10ca');
    gr.query();

    while (gr.next()) {
        var created = new GlideDateTime(gr.getValue('sys_created_on'));
        created.setDisplayValue(created.getDate() + " 00:00:00");

        var diffMillis = today.getNumericValue() - created.getNumericValue();
        var daysOld = Math.floor(diffMillis / (1000 * 60 * 60 * 24));

        if (daysOld === 10) {
            gs.eventQueue('hcl.itam.escalation.10day', gr, gr.sys_id, '10 Day Escalation');
        }

        if (daysOld === 13) {
            gs.eventQueue('hcl.itam.escalation.13day', gr, gr.sys_id, '13 Day Escalation');
        }

    }
})();
3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @Ricardo Sanchez 

 

Your logic is correct, but if you don’t mind, I’d like to share a refactored version of the code

 

(function executeEscalationCheck() {
    var today = new GlideDate();

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('active', true);
    gr.addQuery('cat_item', 'dc9db25e93f0a6108211b22efaba10ca');
    gr.query();

    while (gr.next()) {
        var created = new GlideDate(gr.getValue('sys_created_on'));
        var daysOld = today.getNumericValue() - created.getNumericValue();

        if (daysOld === 10)
            gs.eventQueue('hcl.itam.escalation.10day', gr, gr.sys_id, '10 Day Escalation');

        if (daysOld === 13)
            gs.eventQueue('hcl.itam.escalation.13day', gr, gr.sys_id, '13 Day Escalation');
    }
})();

 

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

Wouldnt i need to convert it to days because getNumericValue() on GlideDate returns the time in milliseconds.


so something like this

(function executeEscalationCheck() {
var today = new GlideDate();

var gr = new GlideRecord('sc_req_item');
gr.addQuery('active', true);
gr.addQuery('cat_item', 'dc9db25e93f0a6108211b22efaba10ca');
gr.query();

while (gr.next()) {
var created = new GlideDate(gr.getValue('sys_created_on'));
var daysOld = Math.floor((today.getNumericValue() - created.getNumericValue()) / (1000 * 60 * 60 * 24));

if (daysOld === 10) {
gs.eventQueue('hcl.itam.escalation.10day', gr, gr.sys_id, '10 Day Escalation');
}

if (daysOld === 13) {
gs.eventQueue('hcl.itam.escalation.13day', gr, gr.sys_id, '13 Day Escalation');
}
}
})();




Ankur Bawiskar
Tera Patron
Tera Patron

@Ricardo Sanchez 

try this

(function executeEscalationCheck() {
    // Get dates for 10 and 13 days ago
    var gd = new GlideDateTime(gs.nowDateTime());
    gd.setDisplayValue(gd.getDate() + " 00:00:00");

    var tenDaysAgo = new GlideDateTime(gd);
    tenDaysAgo.addDaysUTC(-10);

    var thirteenDaysAgo = new GlideDateTime(gd);
    thirteenDaysAgo.addDaysUTC(-13);
    
    var gr = new GlideRecord('sc_req_item');
    gr.addEncodedQuery('active=true^cat_item=dc9db25e93f0a6108211b22efaba10ca^ORsys_created_onON' + tenDaysAgo.getDate() + '^sys_created_onON' + thirteenDaysAgo.getDate());
    gr.query();

    while (gr.next()) {
        // sys_created_onON will fetch records created on that date (regardless of time)
        // To identify if it's 10 or 13 days old:
        var created = new GlideDateTime(gr.getValue('sys_created_on'));
        var daysOld = Math.floor((gd.getNumericValue() - created.getNumericValue()) / (1000 * 60 * 60 * 24));

        if (daysOld === 10) {
            gs.eventQueue('hcl.itam.escalation.10day', gr, gr.sys_id, '10 Day Escalation');
        } else if (daysOld === 13) {
            gs.eventQueue('hcl.itam.escalation.13day', gr, gr.sys_id, '13 Day Escalation');
        }
    }
})();

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