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-07-2025 03:13 AM
I think your custom field is of type date
if yes then try this
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 currentDate = new GlideDateTime();
var threeYearsLater = new GlideDateTime(gr.getValue('u_test_date') + ' 00:00:00');
threeYearsLater.addYearsUTC(3);
if (currentDate.getDate() == threeYearsLater.getDate()) {
// Send email notification
gs.eventQueue('Testing', 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
05-19-2025 10:34 PM
Hope you are doing good.
Did my reply answer your question?
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-06-2025 07:19 AM
Hi @RakshithaM ,
Use below script in your schedule job, make that run on daily basis.
1. You need to update the date with next trigger date, if you will not do then it will trigger first time but for 2nd time onwards it wont trigger.
2. I have tested it in background script and working fine.
// Query records where the Test Date field is not empty
var gr = new GlideRecord('incident'); // Replace with your table name
gr.addQuery('sys_id', '3144a06283fa1210a9589780deaad3eb');
gr.query();
while (gr.next()) {
// Retrieve the Test Date and calculate the date 3 years later
var testDate = new GlideDateTime(gr.getValue('u_date'));
var threeYearsLater = new GlideDateTime(testDate);
threeYearsLater.addYearsUTC(3);
// Get the current date
var currentDate = new GlideDateTime();
// Check if today's date matches the calculated date (3 years later)
if (currentDate.getDate().equals(threeYearsLater.getDate())) {
// Trigger the event to send the email notification
//gs.eventQueue('your_event_name', gr, 'admin@example.com');
gs.print('Event triggered for record: ' + gr.getDisplayValue());
gr.u_date = threeYearsLater.getDate();
gr.setWorkflow(false);
gr.update();
}
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 11:05 PM
Hi @Runjay Patel ,
Notification was getting triggered all time whenever i set TEST DATE irrespective of any Date.
For Test Date field, i have set 7-Jan-2025 and saving it, even for this date, notification was triggered. But i want this to function like when TEST Date is 7-Jan- 2025, then it must trigger notification only on 7-Jan-2028.
Below i have attached code and image as well. Can you please guide me with this.
Test Date - Field Type is Date
Schedule job:
In below code for sys id, i have used sys id of incident table is that correct?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2025 12:24 AM
Hi @Ankur Bawiskar , @Viraj Hudlikar , @Runjay Patel ,
I tried with all the 3 scripts but it's not working accordingly, where emails are getting triggered all time irrespective of date.
But i want emails to be triggered only once on particular date after 3 years.
Example: Presently Test Date is 6-Jan-2025, i want email to be triggered on 6-Jan-2028
Table - Incident
Field - Test Date (Type- Date)
Event - Testing
Thanks in advance.