I have to send birthday notification to employee.

niveditakumari
Mega Sage

Hi, 

 

I have to send birthday notification to employee. I have written schedule job script and date of birth is type as date field. I have tested that script and I'm able to get birthday, birthday day, birthday month value, it seems it is having issue while checking if condition. 

Please find below script and correct that : 

 

var gr = new GlideRecord("sys_user");
//gr.addActiveQuery();
gr.addQuery("active=true^u_sf_date_of_birthISNOTEMPTY");
gr.query();
gs.log('success query');
var c = 0;
while (gr.next()) {
    gs.log('success loop');
    var bdate = gr.u_sf_date_of_birth.toString();
    gs.log('birthday is' + bdate);
    var to_date = new GlideDateTime();//current day's date
    gs.log('today date is' + to_date);
    var bday = bdate.split("-")[2]; //birthday date
    gs.log('birthday day is' + bday);
    var bmonth = bdate.split("-")[1]; //birthday month
    gs.log('birthday month is' + bmonth);
    if(to_date.getMonth().toString() == bmonth && to_date.getDayOfMonth().toString() == bday){
        c++;
        gs.log('count is here' + c);
        gs.log("Hi, it's the user's birthday!");
        gs.eventQueue("birthday.notification",gr,gr.name.toString(),gr.email.toString());
    }
 
Please help me to correct that. 
 
Regards, 
Nivedita 
 
 
1 ACCEPTED SOLUTION

@niveditakumari 

to verify this in HR scope.

Create fix script in HR Core scope and see if the script works

Also don't use gs.log(), it won't work in scoped app, use gs.info()

var gr = new GlideRecord("sn_hr_core_profile");
gr.addQuery("user.active=true^date_of_birthISNOTEMPTY");
gr.query();
while (gr.next()) {
    var bdate = gr.date_of_birth.toString();
    var to_date = new GlideDateTime(); // current day's date
    var bday = bdate.split("-")[2]; // birthday date
    var bmonth = bdate.split("-")[1]; // birthday month

    gs.info('birthday is' + bdate);
    gs.info('today date is' + to_date);
    gs.info('birthday day is' + bday);
    gs.info('birthday month is' + bmonth);

    // Adjust month comparison to account for zero-based index
    if ((to_date.getMonthLocalTime()) == parseInt(bmonth) && to_date.getDayOfMonthLocalTime() == bday) {
        c++;
        gs.info('count is here' + c);
        gs.info("Hi, it's the user's birthday! for " + gr.user.name);
        gs.eventQueue("sn_hr_core.birthday.notification", gr, gr.user.name.toString(), gr.user.email.toString());
    }
} 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

26 REPLIES 26

Ankur Bawiskar
Tera Patron
Tera Patron

@niveditakumari 

try this

var gr = new GlideRecord("sys_user");
gr.addQuery("active=true^u_sf_date_of_birthISNOTEMPTY");
gr.query();
var c = 0;
while (gr.next()) {
    gs.log('success loop');
    var bdate = gr.u_sf_date_of_birth.toString();
    gs.log('birthday is ' + bdate);
    var to_date = new GlideDateTime(); // current day's date
    gs.log('today date is ' + to_date);
    var bday = bdate.split("-")[2]; // birthday date
    gs.log('birthday day is ' + bday);
    var bmonth = bdate.split("-")[1]; // birthday month
    gs.log('birthday month is ' + bmonth);
    
    // Adjust month comparison to account for zero-based index
    if((to_date.getMonth() + 1).toString() == bmonth && to_date.getDayOfMonth().toString() == bday){
        c++;
        gs.log('count is here ' + c);
        gs.log("Hi, it's the user's birthday!");
        gs.eventQueue("birthday.notification", gr, gr.name.toString(), gr.email.toString());
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar

 

Please find below screenshot : 

niveditakumari_0-1739279524189.png 

 

Please help me to correct that. 

 

Regards, 

Nivedita 

 

 

@niveditakumari 

it say field is invalid

are you sure the field name is correct and it's on sys_user table?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar

 

I have executed that code and it is not checking if condition. 

Please find below screenshot : 

niveditakumari_1-1739280521389.png

 

Can you please help me to correct that. 

 

Regards, 

Nivedita 

 

 

@niveditakumari 

this worked for me

var gr = new GlideRecord("sys_user");
gr.addQuery("active=true^u_date_of_birthISNOTEMPTY");
gr.query();
while (gr.next()) {
    var bdate = gr.u_date_of_birth.toString();
    var to_date = new GlideDateTime(); // current day's date
    gs.info('today date is ' + to_date);
    var bday = bdate.split("-")[2]; // birthday date
    gs.info('birthday day is ' + bday);
    var bmonth = bdate.split("-")[1]; // birthday month
    gs.info('birthday month is ' + bmonth);
    
	/*var nowTime = new GlideDateTime();
	var nowMonth = nowTime.getDayOfMonth();
	var date = nowTime.split(' ');
	var day = date.split('-')[2];
	*/

	gs.info('month' + to_date.getMonthLocalTime());
	gs.info('day' + to_date.getDayOfMonthLocalTime().toString());
    // Adjust month comparison to account for zero-based index
    if((to_date.getMonthLocalTime()) == parseInt(bmonth) && to_date.getDayOfMonthLocalTime() == bday){
        gs.info("Hi, it's the user's birthday! for " + gr.name);
    }
}

AnkurBawiskar_0-1739281017205.pngAnkurBawiskar_1-1739281079864.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader