Schedule job script to get field value from user table

Dee S
Giga Guru

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.

1 ACCEPTED SOLUTION

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

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

17 REPLIES 17

Yes, you can use daily or monthly but to test the script you need to run it once.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

For Above script should I run it once or on demand or daily? for current testing purpose

to do testing we need once user whose birthday on this month.

if there is no user with that it will not send email so try to add for user as birthday this month (any date of this month).

So, we have one user then on scheduled job select Run as Once and save. Click on Execute Now Ui action on form.

 

Once it is executed check the logs and emails as well.

on the Emails module you can see all the emails which are triggered and check your email whether it sent or not.

If it is working then you can Run the scheduled job as per the requirement(daily, weekly, monthly or any).

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Tried exactly in logs only current month is showing not picking up birthday month from users table

Hi,

everything is on global scope is that right?

what value you are getting on birthday month log it share the error?

birthday  field type is date is it correct and if yes check backend name and configure the field and share screenshot?

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar