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

J Siva
Tera Sage

Hi @niveditakumari 

Please try the below script. It may work as you expected.
Note: In your script you are using encoded query in the addQuery() method

var user = new GlideRecord("sys_user");
user.addEncodedQuery("active=true^u_hr_hire_dateISNOTEMPTY");
user.query();
while (user.next()) {
    var date = user.getValue("u_subscription_end_date");

    var hire = new GlideDateTime(date);
    var month = hire.getMonthUTC();
    var day = hire.getDayOfMonthUTC();
    var year = hire.getYearUTC();


    var today = new GlideDateTime();
    var month_utc = today.getMonthUTC();
    var day_utc = today.getDayOfMonthUTC();
    var year_utc = today.getYearUTC();


    var d_diff = parseInt(day_utc) - parseInt(day);
    var m_diff = parseInt(month_utc) - parseInt(month);
    var y_diff = parseInt(year_utc) - parseInt(year); // get the anniversary value 1,2,3...

    if (d_diff == 0 && m_diff == 0) {
        gs.print("Yes it's anniversary"); // trigger event
    } else {
        gs.print("NO it's not");
    }

}

Hope this helps.
Regards,
Siva

Hi @J Siva

 

I have tried with your code. It is not working. 

Can you please try to correct my code. 

niveditakumari_0-1742138645292.png

 

niveditakumari_1-1742138697879.png

 

 

Regards, 

Nivedita 

 

 

HI @niveditakumari 
The below scripts are working fine.

var user = new GlideRecord("sys_user");
user.addEncodedQuery("active=true^u_subscription_end_date!=NULL"); //replace with your date field
user.query();
while (user.next()) {
    var date = user.getValue("u_subscription_end_date"); //replace with your date field

    var hire = new GlideDateTime(date);
    var month = hire.getMonthUTC();
    var day = hire.getDayOfMonthUTC();
    var year = hire.getYearUTC();


    var today = new GlideDateTime();
    var month_utc = today.getMonthUTC();
    var day_utc = today.getDayOfMonthUTC();
    var year_utc = today.getYearUTC();


    var d_diff = parseInt(day_utc) - parseInt(day);
    var m_diff = parseInt(month_utc) - parseInt(month);
    var y_diff = parseInt(year_utc) - parseInt(year); // get the anniversary value 1,2,3...

    if (d_diff == 0 && m_diff == 0) {
        gs.info("Yes it's anniversary"); // trigger event
		gs.eventQueue("trigger.anniversary.notification",user);
    } else {
        gs.info("NO it's not");
    }

}
var gr = new GlideRecord("sys_user");
gr.addEncodedQuery("active=true^u_subscription_end_dateISNOTEMPTY");
gr.query();
var c = 0;
while (gr.next()) {
   var jdate = new GlideDateTime(gr.u_subscription_end_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("trigger.anniversary.notification",gr);
   }
}

Please try to remove the timezone from the scheduled job and try again.
Note if you are copy / pasting the code, then just make sure to remove gs.eventQueue("") line and re-write again.

 

Let me know how it goes.
Regards,
Siva

J Siva
Tera Sage

Could you please share your code? (Not a picture)

Also what's the type of hire date field? ( Date / Date&time)