- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-16-2022 01:14 PM
I've created birthday (u_birthday) field in user table (sys_user) which stores users birth date.
I want to send emails to the users whose birthday is in this month only.
var users = new GlideRecord('sys_user');
users.addActiveQuery();
users.query();
while (users.next()){
var birthDay = new GlideDate(users.u_birthday);
var BMonth=birthDay.getMonthNoTZ();
if(BMonth == "9"){
gs.eventQueue('happy_birthday', users, users.sys_id);
}
}
this is the script I've written but it's not working.
I think there is problem in fetching the value from birthday field in users table. Please help me resolve it.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-16-2022 10:59 PM
Hi,
when you are triggering schedule job and try it run On demand?
select one user birthday in this month
var gdt = new GlideDate(); //current date
var currentmonth = gdt.getMonthLocalTime(); //get current month
gs.info("current month " +currentmonth); // check logs on this module system logs>System Log>All
var users = new GlideRecord('sys_user');
users.addActiveQuery();
users.query();
while (users.next()) {
var birthDay = new GlideDate(users.u_birthday); //get birthday date
var bdaymonth = birthDay.getMonthLocalTime(); //get birthday month
gs.info("birthday month " +bdaymonth);//check logs on this module system logs>System Log>All
if (bdaymonth == currentmonth) { //compare current month with birthday month
var sysid = JSON.stringify(users.sys_id);
gs.eventQueue('happy_birthday', users, sysid ,''); //check event name
}
}
using above script check the logs on current and birthday month.
Thanks,
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-16-2022 01:31 PM
Hi Dee,
What happens when you try running this code in a background script? Any error message?
You can the easily use log statements to confirm your values are as expected.
I would also consider using Flow for a no-code/low-code approach.
>>> To help others, please mark this as Correct or Helpful if this response has been of any use. <<<
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-16-2022 07:29 PM
Hi Jon,
Thanks for reply. I checked logs it's showing message as 0 nothing more than that showing.
As I want to take particular month field from Date format I'm using shceduled job script other than flow. Please help me figure it out
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-16-2022 09:44 PM
Hi
please try below script it will work.
Script:
var gdt = new GlideDate(); //current date
var currentmonth = gdt.getMonthLocalTime(); //get current month
var users = new GlideRecord('sys_user');
users.addActiveQuery();
users.query();
while (users.next()) {
var birthDay = new GlideDate(users.u_birthday); //get birthday date
var bdaymonth = birthDay.getMonthLocalTime(); //get birthday month
if (bdaymonth == currentmonth) { //compre current month with birthday month
gs.eventQueue('happy_birthday', users, users.sys_id);
}
}
Hope you it helps you.
Please Mark ā Correct/helpful if applicable, Thanks!!
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-16-2022 09:54 PM
I've tried this but don't know why it is not triggering. I have created u_birthday field on user table it is not by default. Is it because of that?