To send email notification every 3 years
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 06:22 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 06:35 AM
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:
- Navigate to System Definition > Scheduled Jobs.
- Click on New to create a new scheduled job.
- Set the Run field to Daily or any other appropriate frequency.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-08-2025 12:00 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-06-2025 06:38 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 03:01 AM
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');
}
}