can some one please provide me the code for approval reminder through schedule job

Rithvik
Tera Contributor
I need code for approval reminder through schedule job.
1 ACCEPTED SOLUTION

Raghu Ram Y
Kilo Sage

Hi Rithvik,

 

You can try the below workaround code and give a try...

 

    var thirtyDaysAgo = new GlideDateTime();
    thirtyDaysAgo.subtract(30); // Calculate 30 days ago
    
    var approvalGR = new GlideRecord('table_name'); // Replace 'table_name' with the actual table where approvals are stored
    approvalGR.addQuery('state', 'requested');
    approvalGR.addQuery('sys_created_on', '<', thirtyDaysAgo);
    approvalGR.query();
    
    while (approvalGR.next()) {
        // Send a reminder notification to the manager
        // You can use email, a notification, or another preferred method to notify the manager.
    }

Please mark my response as both HELPFUL and CORRECT, if it helps..

 

Regards,

Raghu.

View solution in original post

4 REPLIES 4

Raghu Ram Y
Kilo Sage

Hi Rithvik,

 

You can try the below workaround code and give a try...

 

    var thirtyDaysAgo = new GlideDateTime();
    thirtyDaysAgo.subtract(30); // Calculate 30 days ago
    
    var approvalGR = new GlideRecord('table_name'); // Replace 'table_name' with the actual table where approvals are stored
    approvalGR.addQuery('state', 'requested');
    approvalGR.addQuery('sys_created_on', '<', thirtyDaysAgo);
    approvalGR.query();
    
    while (approvalGR.next()) {
        // Send a reminder notification to the manager
        // You can use email, a notification, or another preferred method to notify the manager.
    }

Please mark my response as both HELPFUL and CORRECT, if it helps..

 

Regards,

Raghu.

Thank you, let me give a try

Rithvik
Tera Contributor

@Raghu Ram Y workaround helped.. thanks.

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Rithvik ,

 

Certainly! To send approval reminders through a scheduled job in ServiceNow, you can use a Scheduled Script Execution. Below is an example of how you can write a scheduled job script to send approval reminders for records waiting for approval:

 

// Scheduled Job Script to Send Approval Reminders

 

// Query records pending approval (adjust the table and conditions based on your use case)

var approvalRecords = new GlideRecord('change_request'); // Example table: change_request

approvalRecords.addQuery('approval', 'requested');

approvalRecords.query();

 

// Set the reminder message and email template

var reminderMessage = 'Reminder: You have pending approval(s) that require your attention.';

var emailTemplate = 'approval_reminder_email_template'; // Replace with the actual email template name

 

// Iterate through the records and send reminders

while (approvalRecords.next()) {

    var approver = approvalRecords.approver.getDisplayValue(); // Get the approver's display name

    var userEmail = approvalRecords.approver.email.getDisplayValue(); // Get the approver's email address

 

    // Send email reminder

    gs.eventQueue('email.send', approvalRecords, userEmail, 'Approval Reminder', null, reminderMessage, emailTemplate);

    

    // Log a message for tracking

    gs.info('Approval reminder sent to ' + approver + ' for record: ' + approvalRecords.number);

}

 

// Log completion

gs.info('Approval reminders sent for pending records.');

 

In this script:

 

- Replace `'change_request'` with the actual table name of the records you want to monitor for approval.

- Adjust the approval conditions in the `addQuery` line to match your specific criteria.

- Modify `approval_reminder_email_template` with the name of the email template you want to use for the reminders.

- Customize the `reminderMessage` variable with your desired reminder text.

 

This script runs as a scheduled job and sends email reminders to the specified approver(s) for records pending approval. Ensure that the scheduled job is configured to run at the desired frequency (for example, daily) in the scheduled job form.

 

Please test the script in a non-production environment first to ensure it works as expected with your specific use case.

 

Thanks,

Danish