Approval Reminders through Scheduled Jobs

achen
Tera Contributor

Hello,


I am trying to create a scheduled job that sends out a notification to approvers who have approvals older than two days.  I am using this script to query and send out the notification but it is not working for some reason.

 

// Define the GlideDateTime for two days ago
var twoDaysAgo = new GlideDateTime();
twoDaysAgo.addDaysUTC(-2);

// Create a GlideRecord for your approval table (e.g., sysapproval_approver)
var approvalGR = new GlideRecord('sysapproval_approver');

// Add a condition to filter records with the state "requested"
approvalGR.addQuery('state', 'requested');

// Add a condition to filter records older than two days
approvalGR.addQuery('sys_created_on', '<', twoDaysAgo);

// Execute the query
approvalGR.query();

while (approvalGR.next()) {
    // Get the approver's user record
    var approverUser = new GlideUser(approvalGR.approver);

    // Construct the notification message
    var notificationMessage = "Dear " + approverUser.getDisplayValue() + ",\n\n";
    notificationMessage += "This is a reminder that you have pending approval tasks in ServiceNow.\n";
    notificationMessage += "These tasks were created more than two days ago and require your attention.\n\n";
    notificationMessage += "Request: " + approvalGR.short_description + "\n";
    notificationMessage += "Created On: " + approvalGR.sys_created_on.getDisplayValue() + "\n";
    notificationMessage += "Due Date: " + approvalGR.due_date.getDisplayValue() + "\n\n";
    notificationMessage += "Please log in to ServiceNow and review these approval tasks as soon as possible. ";
    notificationMessage += "Your timely action is crucial to ensure the smooth flow of processes.\n\n";
    notificationMessage += "Thank you for your prompt attention to this matter.\n\n";
    notificationMessage += "Best regards,\nYour Organization Name";

    // Send the notification using GlideNotification
    var notification = new GlideNotification();
    notification.setSubject("Approval Reminder");
    notification.setText(notificationMessage);
    notification.setRecipients(approverUser.getID());
    notification.insert();

    // Optionally, you can log this action in the approval record
    approvalGR.comments = 'Reminder notification sent to approver.';
    approvalGR.update();
}

 

 

2 ACCEPTED SOLUTIONS

Tai Vu
Kilo Patron
Kilo Patron

Hi @achen ,

Let's try this approach.

1. Define an Event Registry in the Approvals [sysapproval_approver] table.

2. Fire the event in the script. 

// Define the GlideDateTime for two days ago
var twoDaysAgo = new GlideDateTime();
twoDaysAgo.addDaysUTC(-2);

// Create a GlideRecord for your approval table (e.g., sysapproval_approver)
var approvalGR = new GlideRecord('sysapproval_approver');

// Add a condition to filter records with the state "requested"
approvalGR.addQuery('state', 'requested');

// Add a condition to filter records older than two days
approvalGR.addQuery('sys_created_on', '<', twoDaysAgo);

// Execute the query
approvalGR.query();

while (approvalGR.next()) {
    gs.eventQueue('<event_name>', approvalGR);

    // Optionally, you can log this action in the approval record
    approvalGR.comments = 'Reminder notification sent to approver.';
    approvalGR.update();
}

3. Create a Notification with this Event is fired, and add the Approver as the Audience.

 

Let me know if it works for you.

 

Cheers,

Tai Vu

View solution in original post

Hi @achen 

The line 18 from your script. 😜

Changes it from

gs.eventQueue('<open.approval.reminders>', approvalGR);

to

gs.eventQueue('open.approval.reminders', approvalGR);

 

Double-check the event queue to see whether the event fired or not.

URL: https://<instance_name>/sysevent_list.do?sysparm_query=sys_created_onONToday@javascript&colon;gs.beg...

 

Cheers,

Tai Vu

View solution in original post

7 REPLIES 7

Hi @achen 

The line 18 from your script. 😜

Changes it from

gs.eventQueue('<open.approval.reminders>', approvalGR);

to

gs.eventQueue('open.approval.reminders', approvalGR);

 

Double-check the event queue to see whether the event fired or not.

URL: https://<instance_name>/sysevent_list.do?sysparm_query=sys_created_onONToday@javascript&colon;gs.beg...

 

Cheers,

Tai Vu

achen
Tera Contributor

Thank you so much! It works!  I appreciate you!

 

Very thankful,

 

Alex

achen
Tera Contributor

Hello again,

 

I had a question about this because its finding some approvals that are still in the requested state but the change has already been closed.  It is still sending out the approval as well as the comment that a reminder was sent but then it changes the state to no longer required.  Is there any way that it changes the state but does not send the notification and adds a different comment?