Email Notification based on Scheduled job

cjv
Tera Expert

Hello,

Hoping I can get some assistance:

I have a requirement to send a notification to Change Managers when a Standard Change Template is nearing expiration (there is an expiration date field).

The intervals from expiration are:

  1. 20 days

  2. 15 days

  3. 7 days

  4. 6 to 1 (every day until expiration)

  5. 0 - notify that a template has expired

The notification template is:

SUBJ:

Standard change template [template name] will expire in XX days / has expired
BODY:
Hi [Change manager name],
The Standard Change template [template name] in ServiceNow will expire in XX days / has expired, please review the template before [expiry date].”

 

I have created a scheduled job and an event (attached).

 

I am a very new to scripting, would someone be able to assist in how the email notification should be set.

Do I need an email script or can it be done through the email notification form?

2 REPLIES 2

Amit Gujarathi
Giga Sage

HI @cjv ,
I trust you are doing great.

you can achieve this by using a scheduled job and an email script. Let's break it down step by step:

  1. Scheduled Job:

    • Create a scheduled job in ServiceNow that runs at regular intervals (e.g., once a day).
    • This job will check the expiration date of each Standard Change Template and trigger the appropriate email notification.
  2. Email Script:

    • Create an email script that will generate the email notification.
    • Here's an example of how the script might look in ServiceNow's scripting language (JavaScript):

 

// Fetch the Standard Change Templates nearing expiration
var templateGr = new GlideRecord('standard_change_template');
templateGr.addQuery('expiration_date', '>=', gs.daysAgo(-20)); // Adjust the number of days as per your requirements
templateGr.addQuery('expiration_date', '<=', gs.daysAgo(0));
templateGr.query();

while (templateGr.next()) {
    var expirationDate = templateGr.getValue('expiration_date');
    var daysUntilExpiration = gs.daysBetween(gs.nowDateTime(), expirationDate);

    // Determine the appropriate email subject and body based on the expiration interval
    var subject = "Standard change template " + templateGr.getValue('name') + " will expire in " + daysUntilExpiration + " days / has expired";
    var body = "Hi [Change manager name],\n\nThe Standard Change template " + templateGr.getValue('name') + " in ServiceNow will expire in " + daysUntilExpiration + " days / has expired, please review the template before " + expirationDate + ".";
    
    // Send the email to the Change Manager
    gs.eventQueue("email.send", templateGr.getRecordClassName(), templateGr.getUniqueValue(), subject, body, templateGr.getValue('change_manager'));
}
​

 

  • Email Notification Form:
    • You can use the email script within an email notification form to define the content and format of the email being sent.
    • Create an email notification in ServiceNow and reference the email script you created in the previous step.
    • This form allows you to customize the email layout, add additional fields, and control other email settings.
  •  

sourav1999
Mega Guru

Email Notifications can be sent from Scheduled Jobs in ServiceNow

   - This can be done through the use of the GlideSysMail class

   - The GlideSysMail class can be used to set up an email notification to be sent from a Scheduled Job.

   - The GlideSysMail class requires a few parameters to be set up in order to send the notification including the recipient, subject, message, and from address

   - Once the parameters are set up, the GlideSysMail class can be used to send the email notification from the Scheduled Job

 

For asking ServiceNow-related questions try this :

For a good and optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.

Link - https://nowgpt.ai/