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

Hi,

you can fields on user table nop with that.

I think problem with event calling

replace below line in you code

gs.eventQueue('happy_birthday', users, users.sys_id,''); //need to pass 4 parameters on event like above

let me know if it working or not.

Regards

Pavankumar

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

gs.eventQueue('happy_birthday', users, users.sys_id,''); is having only 3 parameters I think event_name, users, users.sys_id what is the 4th one?

Hi Pavan, tried it still not working. I gave gs.log message about gs.eventQueue to test but in logs message is showing as 0

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

I'm need to run it daily. Actual requirement is to send birthday email notifications to users as it is not working I'm trying it month wise.

To send birthday email notifications should I run daily or on demand?