Send Notification on Due Date

Mark Wood
Tera Contributor

 

Hello Experts,

I have written the following code, and I want to send notifications based on the due day. If the due date is 7, I want to send a notification 3 days before that. My code is working, but when the due date is 1, I need to send a notification in the previous month, and the day should be either 28 or 27, depending on the number of days in that month. Can anyone please guide me on how to achieve this?

 

 var DueDayINMonth = 1 // 1st of every month



        var GDT1 = new GlideDateTime();
        var CurrentDate = GDT1.getDayOfMonthUTC();
        gs.print("Print Name of schedule" + gr.u_ma_fld_name);
        if ((CurrentDate + 3) <=DueDayINMonth) {
            gs.print("Due Date Is greater than currentdate");
            if (CurrentDate == DueDayINMonth - 3) {
                gs.print("send Notification");

            }
        }
        var notificationDay1 = DueDayINMonth - 3; // Calculate the day to send the notification (3 days before the 1st)
        gs.print(notificationDay1);
        if (notificationDay1 <= 0) {
            // If notificationDay becomes 0 or negative, adjust it to the last day of the previous month
          
   var lastDayOfPreviousMonth = new GlideDateTime();
		
            lastDayOfPreviousMonth.setMonthUTC(GDT1.getMonthUTC() + 1); // Go back to next one month
            lastDayOfPreviousMonth.setDayUTC(1); // Set the day to the 1st of that month
            lastDayOfPreviousMonth.addDaysUTC(-1); // Subtract 1 day to get the last day of the previous month
            notificationDay1 = lastDayOfPreviousMonth.getDayOfMonthUTC();
			gs.print("notificationDay1"+dayin);
        }

        if (CurrentDate == notificationDay1) {
            gs.print("Days Match Found: Send Notification");
        }

 

3 REPLIES 3

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Mark Wood ,

 

Is due date a custom field or OOTB field?

 

Thanks,

Danish

Mark Wood
Tera Contributor

@Danish Bhairag2 its a custom field that value we are strong in  

DueDayINMonth variable

 

Danish Bhairag1
Tera Contributor

Hi @Mark Wood ,

 

you can create a server-side script using Business Rules or Scheduled Jobs, depending on when you want to trigger the notifications. Here's how you can handle the specific cases you mentioned:

  1. **When the Due Date is 7 Days Away:**

   - If the Due Date is 7 days away, send a notification 3 days before the Due Date.

 

(function executeRule(current, previous /*, other parameters*/) {
    var dueDate = current.due_date; // Replace 'due_date' with your custom field name
    var notificationDate = new GlideDateTime();
    notificationDate.addDays(dueDate - 3); // Send notification 3 days before the due date

    if (notificationDate.equals(new GlideDateTime(), 'day')) {
        // Send notification as notificationDate is the same as the current date
        gs.eventQueue('notification.event', current, notificationDate);
    }
})(current, previous);

 

 

  1. **When the Due Date is 1 Day (Special Case for Previous Month):**

   - If the Due Date is 1 day, send a notification on either the 27th or 28th of the previous month based on the number of days in that month.

 

(function executeRule(current, previous /*, other parameters*/) {
    var dueDate = current.due_date; // Replace 'due_date' with your custom field name
    var today = new GlideDateTime();

    if (dueDate == 1) {
        // Calculate the notification date for the previous month
        var notificationDate = new GlideDateTime();
        notificationDate.setMonthUTC(today.getMonthUTC() - 1); // Previous month
        notificationDate.setDayOfMonthUTC(new Date(today.getYearUTC(), today.getMonthUTC(), 0).getDate()); // Last day of previous month

        if (notificationDate.equals(new GlideDateTime(), 'day')) {
            // Send notification as notificationDate is the same as the current date
            gs.eventQueue('notification.event', current, notificationDate);
        }
    }
})(current, previous);

 

 

In the code above, `notification.event` is a placeholder for the event name you want to use for the notification. You can customize it as per your application's event framework.

 

This script checks the Due Date and calculates the notification date based on the rules you specified. If the calculated notification date matches the current date, it triggers a notification event. Please adjust the event triggering logic as needed based on your specific notification requirements.

 

Mark my answer helpful & accepted if it helps you resolve your query.

 

Thanks,

Danish