To send email notification every 3 years

RakshithaM
Tera Contributor

Hi,

 

I have a requirement where email needs to be sent every 3 years based on the Date.

 

Have created a Event, Notification and field name Test Date where type is Date/Time. 

Example: When 6-Jan-2025 is the date present in the Test Date field, so after 3 years i.e. on 6-Jan-2028 an email must be triggered automatically.

Can someone please help me with this.

11 REPLIES 11

Viraj Hudlikar
Giga Sage

Hello @RakshithaM 

Since you have now event, notification and field created, now go ahead and create a Scheduled Job to trigger the email notification based on the date in the "Test Date" field.

Steps for scheduled job:

  1. Navigate to System Definition > Scheduled Jobs.
  2. Click on New to create a new scheduled job.
  3. Set the Run field to Daily or any other appropriate frequency.
  4. In the Condition field, add a script to check if the current date is exactly 3 years after the "Test Date".

Script in scheduled job will be like 

var gr = new GlideRecord('your_table_name'); // Replace with your table name
gr.addQuery('test_date', '=', gs.daysAgoStart(365 * 3)); // 3 years ago
gr.query();
while (gr.next()) {
    gs.eventQueue('your_event_name', gr, gr.sys_id, gs.getUserID());
}

 

I believe the notification you created is link with event you created, if not please do same.


If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

Thanks & Regards
Viraj Hudlikar. 

@RakshithaM 

Use below code as you specified you are using Date type field.

var gr = new GlideRecord('tableName'); // Replace with your table name
gr.addQuery('FieldName', '!=', ''); // Ensure the Test Date field is not empty
gr.addActiveQuery();
gr.query();
while (gr.next()) {
    
	var threeYearsEarlierDate = new GlideDateTime();
	threeYearsEarlierDate.addYearsUTC(-3);
	 if (gr.fieldName == threeYearsEarlierDate.getDate()) {
     	// Send email notification
        gs.eventQueue('your_event_name', gr, gr.sys_id, gs.getUserID());
    }
    
}

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Ankur Bawiskar
Tera Patron
Tera Patron

@RakshithaM 

you can run schedule job daily and check if today's date is exact 3 years after the Test Date field

what script did you start with and where are you stuck?

Something like this, I hope you know how to configure event, notification and trigger the event from script using gs.eventQueue()

var gr = new GlideRecord('your_table_name'); // Replace with your table name
gr.addQuery('test_date', '!=', ''); // Ensure the Test Date field is not empty
gr.query();
while (gr.next()) {
    var testDate = new GlideDateTime(gr.getValue('test_date'));
    var currentDate = new GlideDateTime();
    var threeYearsLater = new GlideDateTime(testDate);
    threeYearsLater.addYearsUTC(3);
    if (currentDate.getDate() == threeYearsLater.getDate()) {
        // Send email notification
        gs.eventQueue('your_event_name', gr, 'admin@example.com');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi  @Ankur Bawiskar ,

 

In TEST DATE field, i set the date as 7-Jan-2025, even for this date, notification was triggering. But i want this to function like when TEST Date is 7-Jan- 2022, then it must trigger notification in 7-Jan-2025.

 

Below is the scheduled job which i have created and inserted the below code. But it was triggering notification whenever i set TEST DATE irrespective of any Date.

Can you please guide me with this. 

 

Test Date - Field Type is Date

 

Schedule job:

Run- Daily

Timezone- System time zone

Hours- 00

 

var gr = new GlideRecord('incident'); // Replace with your table name
gr.addQuery('u_test_date', '!=', ''); // Ensure the Test Date field is not empty
gr.query();
while (gr.next()) {
    var testDate = new GlideDateTime(gr.getValue('u_test_date'));
    var currentDate = new GlideDateTime();
    var threeYearsLater = new GlideDateTime(testDate);
    threeYearsLater.addYearsUTC(3);
    if (currentDate.getDate() == threeYearsLater.getDate()) {
        // Send email notification
        gs.eventQueue('Testing', gr, 'admin@example.com');
    }
}​