DEV Community

Thom
Thom

Posted on • Edited on

3

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

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

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!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay