I need to send notification on employee work anniversary.

niveditakumari
Mega Sage

Hi, 

 

I need to send notification on employee work anniversary. 

To achieve that I have created : 

1. Event 

2. Schedule Job 

var gr = new GlideRecord('sys_user');
gr.addQuery('active=true^u_hr_hire_dateISNOTEMPTY^nameLIKEnivedita'); 
gr.query();
while(gr.next()) {

    var hireDate = gr.u_hr_hire_date; //this field is date of joining
    var hireMonth = hireDate.substring(5,7); //getting month from date of joining
    var hireDay = hireDate.substring(8,10); //getting day from date of joining
   var todayDate = new GlideDateTime();  //getting today date
    var todayMonth = todayDate.getMonth(); // getting today month
    var dayOfMonth = todayDate.getDayOfMonthLocalTime(); // getting today day
    gs.log('hire date is' + hireDate);
    gs.log('hire month is' + hireMonth);
    gs.log('hire day is' + hireDay);
    gs.log('today date is' + todayDate);
    gs.log('today month is' + todayMonth);
    gs.log('today day is' + dayOfMonth);

    if(hireMonth == todayMonth && hireDay == dayOfMonth) {

        c++;
        gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, '', gr.name);
        gs.log('notification sent');
    }
    gs.log('count is' + c);
}
 
When I'm writing code for nivedita for particular user then it is checking if condition and it is executing code that is written after if condition but when I'm writing code for all user it is not checking if condition. 
var c= 0;
var gr = new GlideRecord('sys_user');
gr.addQuery('active=true^u_hr_hire_dateISNOTEMPTY'); /////////////////////////////////////////////
gr.query();
while(gr.next()) {

    var hireDate = gr.u_hr_hire_date;
    var hireMonth = hireDate.substring(5,7);
    var hireDay = hireDate.substring(8,10);
    var todayDate = new GlideDateTime();  
    var todayMonth = todayDate.getMonth();  
    var dayOfMonth = todayDate.getDayOfMonthLocalTime();
    gs.print('hire date is' + hireDate);
    gs.print('hire month is' + hireMonth);
    gs.print('hire day is' + hireDay);
    gs.print('today date is' + todayDate);
    gs.print('today month is' + todayMonth);
    gs.print('today day is' + dayOfMonth);

    if(hireMonth == todayMonth && hireDay == dayOfMonth) {

        c++;
        gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, '', gr.name);
        gs.print('notification sent');
    }
    gs.print('count is' + c);
 
Can you please help me to correct that code. 
 
Regards, 
Nivedita 
 
 
1 ACCEPTED SOLUTION

Hi @niveditakumari ,

Adjusted the code to below as I wasn't aware that your "Date Of Joining" field was not a date field. 

 

var gr = new GlideRecord("sys_user");
gr.addQuery("active=true^u_date_of_joiningISNOTEMPTY"); // Query for active users with a joining date
gr.query();

while (gr.next()) {
    var jdate = new GlideDateTime(gr.u_date_of_joining); // Parse u_date_of_joining
    var to_date = new GlideDateTime(); // Current date
	gs.print(jdate.getDayOfMonth());

    // Compare day and month
    if (to_date.getMonthUTC() == jdate.getMonthUTC() && to_date.getDayOfMonth() == jdate.getDayOfMonth()) {
        gs.print("Hi, it's the user's joining date anniversary!");
        gs.eventQueue("joining.notification", gr, gr.name.toString(), gr.email.toString());
    }
}

 


Please try using this script and let me know if there are any further issues.

 

Please mark this solution as "Helpful" and "Accepted Solution" if this solution helped you in any way.


Thanks and Regards, 
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India

View solution in original post

32 REPLIES 32

SAI VENKATESH
Tera Sage
Tera Sage

Hi @niveditakumari 

 

You can probably try using Flow Designer; it will be much more easier.

SAIVENKATESH_0-1734296360822.png

then use Lookup Records and filter based upon the conditions you needed. 

 

and then use the Action "send Email

SAIVENKATESH_1-1734296529637.png

 

If you have any questions, please feel free to ask.

 

Thanks and Regards

Sai Venkatesh

 

Sai_Charan_K
Kilo Sage

Hi @niveditakumari ,

If you still wish to go with the scheduled job itself, please find the below script:

Scheduled Job:

var gr = new GlideRecord("sys_user");
//gr.addActiveQuery();
gr.addQuery("active=true^u_date_of_joiningISNOTEMPTY");
gr.query();
while (gr.next()) {
    var jdate = gr.u_date_of_joining.toString();
    var to_date = new GlideDateTime();//current day's date
    var jday = jdate.split("-")[2]; //joining date
	var jmonth=jdate.split("-")[1]; //joining month
	if(to_date.getMonth().toString()==jmonth &&to_date.getDayOfMonth().toString()==jday){
		gs.eventQueue("joining.notification",gr,gr.name.toString(),gr.email.toString());
	}
}

 
Attached are the screenshots of the event logs that got triggered depending upon the joining dates of the user stored in user table.

This code is tested and works fine for me, So please do give it a try.

Please mark this solution as "Helpful" and "Accepted Solution" if this solution helped you in any way.


Thanks and Regards, 
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India

Hi @Sai_Charan_K

 

I have used yourt code but it is not working. 

Please find attached screenshot : 

 

Notification(When to send) 

niveditakumari_0-1734332480557.png 

 

Who will receive

niveditakumari_1-1734332613489.png 

 

Background Script print 

niveditakumari_2-1734332843193.png  

 

Background Script 

var gr = new GlideRecord("sys_user");
//gr.addActiveQuery();
gr.addQuery("active=true^u_hr_hire_dateISNOTEMPTY");
gr.query();
gs.print('hire date is');
while (gr.next()) {
    var jdate = gr.u_hr_hire_date.toString();
    var to_date = new GlideDateTime();//current day's date
    var jday = jdate.split("-")[2]; //joining date
    var jmonth=jdate.split("-")[1]; //joining month
    if((to_date.getMonth().toString()==jmonth) && (to_date.getDayOfMonth().toString()==jday)){
        gs.eventQueue("sn_hr_core.work.anniversary.notification",gr,gr.name.toString(),gr.email.toString());
        gs.print('notification sent to employee');
    }
 
Code is not working. It is not checking if condition. 
Can you please correct that. 
 
Regards, 
Nivedita 
 
 

Can you try printing jday and jmonth values in background script ?