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

 

I have printed Jday, Jmonth. 

niveditakumari_0-1734333847873.png 

 

Regards, 

Nivedita

 

 

 

Hi @Sai_Charan_K

 

I'm printing Jday,Jmonth. It is printing Jmonth correctly but in Jday it is taking day and time. 

Can you please help me to correct that. 

 

Regards, 

Nivedita 

 

 

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

Hi @Sai_Charan_K

 

Thank you so much for all your help. It is working as expected. 

It is printing correct in background script but when I'm executing that in schedule job manually it is not showing logs with notification subject line in emails and logs in events. 

Can you please help me for that. 

 

Regards, 

Nivedita A

 

 

Hi @niveditakumari ,

I think this is because of the cross scope privileges issues.

Did you try checking the giving the cross scope privileges? If not please do as you are access global scope data from HR scope. Please go to the table : sys_scope_privilege.LIST and check for the record with your schedule job name or create a new record under this table to allow the access.

Please mark this solution as "Helpful" and please mark it "Correct" and close the thread so that it benefits future readers.


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