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 @Ankur Bawiskar

 

Can you please make me understand that line 

gr.addEncodedQuery('active=true^u_hr_hire_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');

 What this complete script is doing. 

 

Regards, 

Nivedita 

 

 

 

@niveditakumari 

change will be required in above script

update as this

var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.addQuery('u_hr_hire_date', 'ISNOTEMPTY');
while (gr.next()) {
    var hireDate = new GlideDateTime(gr.u_hr_hire_date);
    var today = new GlideDateTime();
    if (hireDate.getMonth() == today.getMonth() && hireDate.getDayOfMonth() == today.getDayOfMonth()) {
        gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, '', gr.name);
    }
}

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

 

I have written code but it is not working. It is not checking if condition. It is printing hire date is but not printing notification is sent to employee. 

niveditakumari_3-1734333185635.png 

 

niveditakumari_4-1734333311475.png

 

Can you please help me to correct code. 

 

Regards, 

Nivedita 

 

 

@niveditakumari 

try this

Also make sure notification and event are on sys_user table

Is that user having notification preference enabled?

var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.addQuery('u_hr_hire_date', 'ISNOTEMPTY');
while (gr.next()) {
    var hireDate = new GlideDateTime(gr.u_hr_hire_date);
    var today = new GlideDateTime();
    if (hireDate.getMonth() == today.getMonth() && hireDate.getDayOfMonth() == today.getDayOfMonth()) {
        gs.eventQueue('sn_hr_core.work.anniversary.notification', gr, gr.email.toString(), gr.getDisplayValue());
    }
}

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

 

It is not working. 

niveditakumari_1-1734334891190.png

 

niveditakumari_2-1734334927336.png

 

Can you please help me to correct that. 

 

Regards, 

Nivedita