I need to send notification to user on user anniversary.

niveditakumari
Mega Sage

Hi @Ankur Bawiskar

 

I'm executing my schedule job now and I'm able to see that in email logs it is sending email notification for user whom hire date is on 17th March ideally it should send notification to user whom hire date is on today 16th March but it is taking that user whom hire date is on tomorrow that is 17 March and sending notification to that user. I need to send notification to user when their anniversary date is completed and we are calculating anniversary date based on hire date and it should send notification user whom hire date is on today. 

It should calculate user anniversary for user whom hire date is on today that is 16th March but is calculating user anniversary whom hire date is on tomorrow that ids 17th March. 

I need to calcualte anniversary date on exact hire date. 

 

I have written script : 

Schedule job 

niveditakumari_0-1742121341083.png 

 

Please help me to correct that So that it will send notification to user whom hire date is on today only. 

 

Regards, 

Nivedita 

 

 

 

24 REPLIES 24

Hello @niveditakumari 

sys_user table records:

vishal_jaswal_1-1742236531942.png


Your Code Results - The hire date timestamp is different as it is in GMT

vishal_jaswal_0-1742236496439.jpeg


Updated Code:

 

var gr = new GlideRecord("sys_user");
gr.addQuery("active=true^u_hr_hire_dateISNOTEMPTY");
gr.query();
var c = 0;
while (gr.next()) {
    var jdate = new GlideDateTime(gr.u_hr_hire_date); // Parse u_hr_hire_date  
    var to_date = new GlideDateTime(); // Current system date & time
    // Get the date as per system time zone
    var jdateDisplay = jdate.getDisplayValue();
    var to_dateDisplay = to_date.getDisplayValue();
    gs.info('Hiring date for user: ' + gr.name.toString() + ' | ' + jdateDisplay);
    gs.info('Hiring date exact timestamp: ' + jdateDisplay);
    gs.info('Hiring day: ' + jdate.getDayOfMonthLocalTime());
    gs.info('Hiring month: ' + jdate.getMonthLocalTime());
    gs.info('Today’s date: ' + to_dateDisplay);
    gs.info('Today’s day: ' + to_date.getDayOfMonthLocalTime());
    gs.info('Today’s month: ' + to_date.getMonthLocalTime());
    // Compare day and month in local time zone
    if (to_date.getMonthLocalTime() == jdate.getMonthLocalTime() &&
        to_date.getDayOfMonthLocalTime() == jdate.getDayOfMonthLocalTime()) {
        c++;
        gs.log('Anniversary count: ' + c);
        gs.log("Happy work anniversary, " + gr.name.toString() + "!"  + '\n' + '\n');
        gs.eventQueue("work.anniversary.notification", gr, gr.name.toString(), gr.email.toString());
    }
}

 

 

vishal_jaswal_2-1742236713246.png

 

vishal_jaswal_3-1742236725456.png

 


Hope that helps!

Hi @Vishal Jaswal

 

Thank you so much it is working as expected. 

Can you please help me with birthday notification, same thing happeining in that and change that as you have done for anniversary and please make me understand what was the issue. 

 

Please find my code : 

var c= 0;
var gr = new GlideRecord("sn_hr_core_profile");
gr.addQuery("user.active=true^date_of_birthISNOTEMPTY");
gr.query();
while (gr.next()) {
    var bdate = gr.date_of_birth.toString();
    var to_date = new GlideDateTime(); // current day's date
    var bday = bdate.split("-")[2]; // birthday date
    var bmonth = bdate.split("-")[1]; // birthday month

    gs.info('birthday is' + bdate);
    gs.info('today date is' + to_date);
    gs.info('birthday day is' + bday);
    gs.info('birthday month is' + bmonth);

    // Adjust month comparison to account for zero-based index
    if ((to_date.getMonthLocalTime()) == parseInt(bmonth) && to_date.getDayOfMonthLocalTime() == bday) {
        c++;
        gs.info('count is here' + c);
        gs.info("Hi, it's the user's birthday! for " + gr.user.name);
        gs.eventQueue("sn_hr_core.birthday.notification", gr, gr.user.name.toString(), gr.user.email.toString());
    }
 
Regards, 
Nivedita 
 
 

Hi @Vishal Jaswal

 

My notification is working as expected. 

Can you please make me understand what was the isuee and where exactly did you made changes for that. 

 

Regards, 

Nivedita 

 

 

Hello @niveditakumari 

I assume the birthday notification is working as expected and you don't need further assistance, right?

From the hire date screenshot you shared earlier, I noticed that in your instance system property "glide.sys.date_form" has value "MM-dd-yyyy". This can mean that your background script is providing results as per the GMT timezone which is either ahead or behind the hire date 

So, I added two variables to capture the displayValue() of u_hr_hire_date field and current date as shown below and then kept using these two variables in the remaining code.

var jdate = new GlideDateTime(gr.u_hr_hire_date); // Parse u_hr_hire_date  
    var to_date = new GlideDateTime(); // Current system date & time
    // Get the date as per system time zone
    var jdateDisplay = jdate.getDisplayValue();
    var to_dateDisplay = to_date.getDisplayValue();


I would request you to please mark that specific message as Accepted Solution and if you won't mind then please mark helpfuls for all those messages who assisted here (I believe everyone). 


Hope that helps!

@niveditakumari 

can you try this

    var jdate = new GlideDateTime(gr.u_hr_hire_date.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