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

 

Should I create notification and schedule job in global scope as I'm accessing global data. 

Can you please confirm on that. 

 

Regards, 

Nivedita 

 

 

Hi @niveditakumari ,

Yes you can create notification and schedule job in the global scope.

 

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

Hi @Sai_Charan_K

 

Should I create notification and schedule job in global scope. 

Can you please confirm on that. 

 

Regards, 

Nivedita

 

 

Hi @niveditakumari ,

You can either do that or you can give cross scope privileges.

 

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

Hi @Sai_Charan_K

 

Can you please confirm how to create cross scope to provide access for that. 

 

Regards, 

Nivedita