DEV Community

Thom
Thom

Posted on • Edited 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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
kamal_deeppareek_f5bb5d8 profile image
Kamal Deep Pareek

Great solution for automating approval reminders in ServiceNow! Leveraging INSTANCEOF and properties for flexible configuration is smart. The approach to adjust reminder dates based on comments is a thoughtful addition. With slight optimizations, such as error handling and query refinements, this solution can be even more robust. Nice work!