DEV Community

Thom
Thom

Posted on • Updated on

ServiceNow: scheduled job, sending a reminder for approvals

I needed to write a scheduled job, which sends out a reminder notification to the approving person for an approval every x days.

The integer for the days is saved as a property. On the approval table I added a field called 'u_reminder_date' which is filled, when the approval is created. The reminder shall only be send for approvals that are either for requests or requested items. Here I learned, that the addQuery 'INSTANCEOF' can be used perfectly!

Following is the full script:

var approvalGR = new GlideRecord('sysapproval_approver');

approvalGR.addEncodedQuery('u_reminder_date', gs.now());
approvalGR.addQuery('state', 'requested');
approvalGR.addQuery('sysapproval.sys_class_name', 'INSTANCEOF', 'sc_req_item');


approvalGR.query();


var reminderDate = new GlideDateTime();

reminderDate.addDaysUTC(gs.getProperty('approval.reminder.days'));


while (approvalGR.next()) {
    gs.eventQueue('reminder.notification', approvalGR, approvalGR.approver.getValue('email'), '');
    approvalGR.u_reminder_date.setValue(reminderDate);
    approvalGR.update();

}
Enter fullscreen mode Exit fullscreen mode

It can also happen, that a user writes a comment, that he needs more time for the approval. In that case, I added a business rule on the approval table, which is fired then a comment is made. When the commenter is the same as the approver, the reminder date is push back for x days.

if (gs.getUserID() == current.getValue('approver') && current.state == previous.state) {

        var currentDate = new GlideDateTime();
        currentDate.addDaysUTC(gs.getProperty('approval.reminder'));
        current.u_reminder_date.setValue(currentDate);
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)