- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2024 10:08 AM
Hi,
I need to send notification on employee work anniversary.
To achieve that I have created :
1. Event
2. Schedule Job
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2024 12:12 AM - edited 12-16-2024 12:37 AM
Hi @niveditakumari ,
Adjusted the code to below as I wasn't aware that your "Date Of Joining" field was not a date field.
var gr = new GlideRecord("sys_user");
gr.addQuery("active=true^u_date_of_joiningISNOTEMPTY"); // Query for active users with a joining date
gr.query();
while (gr.next()) {
var jdate = new GlideDateTime(gr.u_date_of_joining); // Parse u_date_of_joining
var to_date = new GlideDateTime(); // Current date
gs.print(jdate.getDayOfMonth());
// Compare day and month
if (to_date.getMonthUTC() == jdate.getMonthUTC() && to_date.getDayOfMonth() == jdate.getDayOfMonth()) {
gs.print("Hi, it's the user's joining date anniversary!");
gs.eventQueue("joining.notification", gr, gr.name.toString(), gr.email.toString());
}
}
Please try using this script and let me know if there are any further issues.
Please mark this solution as "Helpful" and "Accepted Solution" if this solution helped you in any way.
Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2024 10:42 PM
Hi @Ankur Bawiskar,
Can you please make me understand that line
gr.addEncodedQuery('active=true^u_hr_hire_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
What this complete script is doing.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2024 10:48 PM
change will be required in above script
update as this
var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.addQuery('u_hr_hire_date', 'ISNOTEMPTY');
while (gr.next()) {
var hireDate = new GlideDateTime(gr.u_hr_hire_date);
var today = new GlideDateTime();
if (hireDate.getMonth() == today.getMonth() && hireDate.getDayOfMonth() == today.getDayOfMonth()) {
gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, '', gr.name);
}
}
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
12-15-2024 11:15 PM
Hi @Ankur Bawiskar,
I have written code but it is not working. It is not checking if condition. It is printing hire date is but not printing notification is sent to employee.
Can you please help me to correct code.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-15-2024 11:27 PM
try this
Also make sure notification and event are on sys_user table
Is that user having notification preference enabled?
var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.addQuery('u_hr_hire_date', 'ISNOTEMPTY');
while (gr.next()) {
var hireDate = new GlideDateTime(gr.u_hr_hire_date);
var today = new GlideDateTime();
if (hireDate.getMonth() == today.getMonth() && hireDate.getDayOfMonth() == today.getDayOfMonth()) {
gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, gr.email.toString(), gr.getDisplayValue());
}
}
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
12-15-2024 11:42 PM