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();
}
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);
}
Top comments (2)
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!
Nice blog and informative content,
Thanks for sharing with us,
We are providing Best SERVICENOW Training in Hyderabad,
SERVICENOW Online Training in Hyderabad
SERVICENOW Training in Hyderabad
SERVICENOW Course in Hyderabad