- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 04:00 AM
Hi,
I have to send birthday notification to employee. I have written schedule job script and date of birth is type as date field. I have tested that script and I'm able to get birthday, birthday day, birthday month value, it seems it is having issue while checking if condition.
Please find below script and correct that :
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 11:46 PM
to verify this in HR scope.
Create fix script in HR Core scope and see if the script works
Also don't use gs.log(), it won't work in scoped app, use gs.info()
var gr = new GlideRecord("sn_hr_core_profile");
gr.addQuery("user.active=true^date_of_birthISNOTEMPTY");
gr.query();
while (gr.next()) {
var bdate = gr.date_of_birth.toString();
var to_date = new GlideDateTime(); // current day's date
var bday = bdate.split("-")[2]; // birthday date
var bmonth = bdate.split("-")[1]; // birthday month
gs.info('birthday is' + bdate);
gs.info('today date is' + to_date);
gs.info('birthday day is' + bday);
gs.info('birthday month is' + bmonth);
// Adjust month comparison to account for zero-based index
if ((to_date.getMonthLocalTime()) == parseInt(bmonth) && to_date.getDayOfMonthLocalTime() == bday) {
c++;
gs.info('count is here' + c);
gs.info("Hi, it's the user's birthday! for " + gr.user.name);
gs.eventQueue("sn_hr_core.birthday.notification", gr, gr.user.name.toString(), gr.user.email.toString());
}
}
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
02-11-2025 06:00 AM
Hi @Ankur Bawiskar,
I have checked in table that field is coming from table('sn_hr_core_profile').
Can you please confirm how we can achieve that I know we can glide sn_hr_core_profile tabel and we need to send email to user then on which table we should write code.
Can you please confirm code for that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 06:07 AM
table should be HR profile and not sys_user
so update as this. I hope you can take it from here i.e. trigger event, configure notification etc and recipient and body
1) event on HR profile
2) Notification also on HR Profile and link with above event
a) Event parm1 contains Recipient - true
var gr = new GlideRecord("sn_hr_core_profile");
gr.addQuery("user.active=true^u_date_of_birthISNOTEMPTY");
gr.query();
while (gr.next()) {
var bdate = gr.u_date_of_birth.toString();
var to_date = new GlideDateTime(); // current day's date
var bday = bdate.split("-")[2]; // birthday date
var bmonth = bdate.split("-")[1]; // birthday month
// Adjust month comparison to account for zero-based index
if ((to_date.getMonthLocalTime()) == parseInt(bmonth) && to_date.getDayOfMonthLocalTime() == bday) {
gs.info("Hi, it's the user's birthday! for " + gr.user.name);
gs.eventQueue("birthday.notification", gr, gr.user.email.toString());
}
}
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
02-11-2025 08:35 AM
HI @Ankur Bawiskar,
I have tried above code in backgroung script, it is giving error for cross scope access.
Please find below screenshot :
I'm just testing this code in background script.
Can you please help me to test that.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 10:00 AM
Hi @Ankur Bawiskar,
I have created event, notification and schedule job script all in Human Resources : Core scope and I have tested your above code, it is printing birthday, today date, birthday day and birthday month, it seems it is not checking if condition. It is not printing log after if condition. It is not sending notification to employee.
1. HR Profile table
2. HR Profile table field name
3. Code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2025 06:56 PM
the script worked for me.
Do you have users with birthday on that day in HR Profile table?
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