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

Hi @Sai_Charan_K

 

Can you please help me to create cross scope privileges. 

 

Regards, 

Nivedita 

 

 

Hi @Sai_Charan_K

 

It is sending notification who is completing their anniversary on tomorrow.

Ex - Today is 4th Jan and I'm executing schedule job today then it is checking for tomorrow's hire date and sending notification for that user who is completing their anniversary on 5th Jan.

Can you please help me to fix that. 

 

Regards, 

Nivedita 

 

 

Hi @niveditakumari ,

Can you please share the script and scheduled job conditions which you have used ?

 

Thanks and Regards,

K. Sai Charan

Hi @Sai_Charan_K

 

I have tested that and found that after executing schedule job it is sending notification whose is completing anniversary on 3rd Jan. I need to send notification only for exact anniversary. It is sending notification to +1 day like when I'm executing that today it is sending notification for today + 1 that is completing their anniversary on 3rd Jan but it should send notification who is completing anniversary on today. 

Please find attached screenshot : 

niveditakumari_0-1735811855541.png

 

Can you please help me to send notification for exact day. 

 

Regards, 

Nivedita 

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@niveditakumari 

you don't need to do this much string manipulation to get the month and day

simply use filter condition like this

var c = 0;
var gr = new GlideRecord('sys_user');
gr.addEncodedQuery('active=true^u_hr_hire_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
gr.query();
while (gr.next()) {
    c++;
    gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, '', gr.name);
}
gs.print('count is' + c);

 

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