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 Deepika,

why comments are deleted?

 

Regards,

Pavankumar

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

Mahendra RC
Mega Sage

Hello @Dee S ,

Please check if the below script helps to trigger the Happy Birthday email on the date of user's birthday:

(function () {
	var gdt = new GlideDateTime(); //get today's date time
	var todayDate = gdt.getDate(); //get today's date
	gs.info("today date: " + todayDate); // check logs on System logs --> All
	var query = "u_birthdayON" + todayDate +"@javascript:gs.dateGenerate('" + todayDate + "','start')@javascript:gs.dateGenerate('" + todayDate + "','end')";;
	var userRecordGR = new GlideRecord('sys_user');
	userRecordGR.addActiveQuery();
	userRecordGR.addEncodedQuery(query);
	userRecordGR.query();
	while (userRecordGR.next()) {
		gs.eventQueue('happy_birthday', userRecordGR, userRecordGR.getUniqueValue() ,''); //check event name
	}
})();

If my answer helped you in any way then please do mark it as helpful. If it answered your question then please mark it correct and helpful. This will help others with similar issue to find the correct answer.

Thanks

Hello @Dee S 

Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.

Thanks