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

Jon23
Mega Sage

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. <<<

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

Pavankumar_1
Mega Patron

Hi @Dee S 

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

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

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?