Send notification prior to 90,60 and 30days of a date selected on a variable in a catalog item

Sona2
Tera Contributor

Hi all,

I have a a field on a catalog item called risk ending date(It can be either date or date/time type). I need to send an email notification before 90th,60th and 30th days of it's ending date. I used this script in the timer activity but the timer starts and ends immediately(within fraction of seconds) . Please help me with to set the timer to trigger when left with 30days to the risk ending date. TIA.

var gdt = new GlideDateTime(current.variables.risk_ending_date);
gdt.addDays(-30);
answer = gs.dateDiff(gs.nowDateTime(), gdt, true);

3 REPLIES 3

Mark Manders
Mega Patron

Move away from the workflow and create a flow through flow designer. You can just configure your wait conditions in there.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Dnyaneshwaree
Mega Sage

Hello @Sona2 ,

To achieve the desired notification timing in ServiceNow, you can use scheduled jobs or scheduled notifications instead of a timer activity, which is not designed for long-term scheduling. Here's how you can set up scheduled notifications for 90, 60, and 30 days before the risk_ending_date:

  1. Create Scheduled Notifications:

    • Notification for 90 Days Before:
      • Create a scheduled notification with a condition that triggers 90 days before the risk_ending_date.
    • Notification for 60 Days Before:
      • Create another scheduled notification for 60 days before the risk_ending_date.
    • Notification for 30 Days Before:
      • Create the final scheduled notification for 30 days before the risk_ending_date.
  2. Scheduled Script Execution: You can use a Scheduled Script Execution to calculate the days remaining until the risk_ending_date and send notifications accordingly.

Here's an example of a scheduled script that you can use:

 

var gr = new GlideRecord('sc_req_item'); // Change to your catalog item table if different
gr.addNotNullQuery('risk_ending_date');
gr.query();

while (gr.next()) {
    var riskEndingDate = new GlideDateTime(gr.getValue('risk_ending_date'));
    var now = new GlideDateTime();
    var diff = gs.dateDiff(now.getDisplayValue(), riskEndingDate.getDisplayValue(), true);
    
    if (diff <= 90 && diff > 89) {
        // Send email for 90 days
        gs.eventQueue('risk.notification.90days', gr, gr.sys_id, gr.request.requested_for);
    }
    
    if (diff <= 60 && diff > 59) {
        // Send email for 60 days
        gs.eventQueue('risk.notification.60days', gr, gr.sys_id, gr.request.requested_for);
    }
    
    if (diff <= 30 && diff > 29) {
        // Send email for 30 days
        gs.eventQueue('risk.notification.30days', gr, gr.sys_id, gr.request.requested_for);
    }
}

 

 
  1. Event Registration:
    • Register the events risk.notification.90days, risk.notification.60days, and risk.notification.30days in the Event Registry.
    • Create email notifications linked to these events.


Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!

Dnyaneshwaree Satpute
Tera Guru

If you are going to share AI generated answers, please validate them before sharing. The date difference will be calculated in milli seconds, how is this ever going to work?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark