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

Hi @J Siva

 

Please find my code : 

var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("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 date
    gs.log(jdate.getDayOfMonth());

    // Compare day and month
    if (to_date.getMonthUTC() == jdate.getMonthUTC() && to_date.getDayOfMonth() == jdate.getDayOfMonth()) {
        c++;
        gs.log('count is' + c);
        gs.log("Hi, it's the user's joining date anniversary!");
        gs.eventQueue("work.anniversary.notification", gr, gr.name.toString(), gr.email.toString());
    }
 
My field is Date/Time field.
 
Regards,
Nivedita 
 
 

Hello @niveditakumari 

Your Script is almost correct and I did a minor change:

 

var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("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 date
   gs.log(jdate.getDayOfMonth()); // Log the join date day
   // Compare day and month
   if (to_date.getMonthUTC() == jdate.getMonthUTC() && to_date.getDayOfMonth() == jdate.getDayOfMonth()) {
       c++;
       gs.log('count is ' + c);
       gs.log("Hi, it's the user's joining date anniversary!");
       // Ensure event parameters are correct and not using toString() for already string fields
       gs.eventQueue("work.anniversary.notification", gr, gr.name, gr.email);
   }
}

 

Results:

vishal_jaswal_0-1742147217037.png


Table:sys_user.LIST

 

vishal_jaswal_1-1742147699301.png

 

Additional Information:

Sample Event Registration (Table: sysevent_register)

vishal_jaswal_0-1742146985574.png
Sample Email Notification (Table: sysevent_email_action)

vishal_jaswal_1-1742147034181.png

Who will Receive:

vishal_jaswal_2-1742147045360.png
What it will contain

vishal_jaswal_3-1742147071355.png


Test Results:

vishal_jaswal_4-1742147088470.png

Table:sys_email

vishal_jaswal_5-1742147109707.png

 

vishal_jaswal_6-1742147129661.png

The same logic will work fine for multiple users:

vishal_jaswal_1-1742147560927.png

vishal_jaswal_2-1742147568267.png


vishal_jaswal_0-1742147550971.png

vishal_jaswal_3-1742147595237.png

 

vishal_jaswal_4-1742147606166.png

 


Hope it helps!

 


Hope that helps!

Hi @Vishal Jaswal

 

I have already written same script and it is sending notification to user whom hire date is on tomorrow not today. 

Do I need to to correct my time zone in schedule job. 

Can you please correct that. 

 

Regards, 

Nivedita 

 

 

Hello @niveditakumari 

If you look at the screenshots I shared above, the script is calculating today's date which is March 16. There are 2 users with hire date as of March 16 and e-mail notification is sent to both these users. 

If your background script showb below gives you March 16 then you don't need to modify the timezone

gs.print(gs.now());

vishal_jaswal_0-1742150908841.png


Requesting you to please try the script I provided and share your results.




Hope that helps!

Medi C
Giga Sage

Hi @niveditakumari,

Please try the following (I am resetting the time values for both dates to 00:00:00 when comparing):

var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("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 jDateTime = new GlideDateTime(jdate.getDate() + " 00:00:00");
    var to_date = new GlideDateTime(new GlideDate() + " 00:00:00"); // Current date

    var dur = new GlideDuration();
    dur = GlideDateTime.subtract(jDateTime, to_date);
    var days = dur.getDayPart();

    // Compare day and month
    if (days == "0") {
        c++;
        gs.log('count is' + c);
        gs.log("Hi, it's the user's joining date anniversary!");
        gs.eventQueue("work.anniversary.notification", gr, gr.name.toString(), gr.email.toString());
    }
}

 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.