I need to send notification to user on user anniversary.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 03:36 AM
Hi @Ankur Bawiskar,
I'm executing my schedule job now and I'm able to see that in email logs it is sending email notification for user whom hire date is on 17th March ideally it should send notification to user whom hire date is on today 16th March but it is taking that user whom hire date is on tomorrow that is 17 March and sending notification to that user. I need to send notification to user when their anniversary date is completed and we are calculating anniversary date based on hire date and it should send notification user whom hire date is on today.
It should calculate user anniversary for user whom hire date is on today that is 16th March but is calculating user anniversary whom hire date is on tomorrow that ids 17th March.
I need to calcualte anniversary date on exact hire date.
I have written script :
Schedule job
Please help me to correct that So that it will send notification to user whom hire date is on today only.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 08:39 AM
Hi @J Siva,
Please find my code :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 10:45 AM - edited 03-16-2025 10:56 AM
Hello @niveditakumari
Your Script is almost correct and I did a minor change:
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("active=true^u_hr_hire_dateISNOTEMPTY");
gr.query();
var c = 0;
while (gr.next()) {
var jdate = new GlideDateTime(gr.u_hr_hire_date); // Parse u_hr_hire_date
var to_date = new GlideDateTime(); // Current date
gs.log(jdate.getDayOfMonth()); // Log the join date day
// Compare day and month
if (to_date.getMonthUTC() == jdate.getMonthUTC() && to_date.getDayOfMonth() == jdate.getDayOfMonth()) {
c++;
gs.log('count is ' + c);
gs.log("Hi, it's the user's joining date anniversary!");
// Ensure event parameters are correct and not using toString() for already string fields
gs.eventQueue("work.anniversary.notification", gr, gr.name, gr.email);
}
}
Results:
Table:sys_user.LIST
Additional Information:
Sample Event Registration (Table: sysevent_register)
Sample Email Notification (Table: sysevent_email_action)
Who will Receive:
What it will contain
Test Results:
Table:sys_email
The same logic will work fine for multiple users:
Hope it helps!
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 10:59 AM
Hi @Vishal Jaswal,
I have already written same script and it is sending notification to user whom hire date is on tomorrow not today.
Do I need to to correct my time zone in schedule job.
Can you please correct that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 11:49 AM
Hello @niveditakumari
If you look at the screenshots I shared above, the script is calculating today's date which is March 16. There are 2 users with hire date as of March 16 and e-mail notification is sent to both these users.
If your background script showb below gives you March 16 then you don't need to modify the timezone
gs.print(gs.now());
Requesting you to please try the script I provided and share your results.
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2025 06:03 PM
Hi @niveditakumari,
Please try the following (I am resetting the time values for both dates to 00:00:00 when comparing):
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("active=true^u_hr_hire_dateISNOTEMPTY");
gr.query();
var c = 0;
while (gr.next()) {
var jdate = new GlideDateTime(gr.u_hr_hire_date); // Parse u_hr_hire_date
var jDateTime = new GlideDateTime(jdate.getDate() + " 00:00:00");
var to_date = new GlideDateTime(new GlideDate() + " 00:00:00"); // Current date
var dur = new GlideDuration();
dur = GlideDateTime.subtract(jDateTime, to_date);
var days = dur.getDayPart();
// Compare day and month
if (days == "0") {
c++;
gs.log('count is' + c);
gs.log("Hi, it's the user's joining date anniversary!");
gs.eventQueue("work.anniversary.notification", gr, gr.name.toString(), gr.email.toString());
}
}
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.