Send an email 15 Minutes prior to Change planned start date/time

imran rasheed
Tera Contributor

Hi. I am trying to set up an email notification that will be sent to the assignee of a change 15 Minutes prior to the planned start date. To do this I've attempted to create a Scheduled Job that runs hourly, checks for records where the planned start date is 15 Minutes away and then creates an Event. The event should then trigger the email notification. However, I'm having trouble figuring out how to work out the '15 Minutes from now' part. When i filter it out, it shows all the upcoming changes. Not only the ones which is going to start in 15 minutes time.

 

This is my script in the Scheduled Job:

 

var gr = new GlideRecord('change_request');


gr.addActiveQuery('active', 'true'); //It will fetch all Active Records


gr.addEncodedQuery('start_dateRELATIVEGT@minute@ahead@15');


gr.query();


while(gr.next()) {


gs.eventQueue("change.remainder", gr, "", "");


}

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Try like this.

var gr = new GlideRecord('change_request');
gr.addActiveQuery('active', 'true'); //It will fetch all Active Records
gr.query();
while(gr.next()) {
var gdt = new GlideDateTime(gr.start_date);
gdt.addSeconds(-900);
var gdt1 = new GlideDateTime();
  if(gdt.equals(gdt1)) {
    gs.eventQueue("change.remainder", gr, "", "");
  }
}

Or , you can try another approach.

In the BR after insert, trigger a event like below.

var gdt = new GlideDateTime(current.start_date); //put actual field name
gdt.addSeconds(-900);
gs.eventQueueScheduled("change.remainder", current, "", "",gdt);

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

24 REPLIES 24

Hello Imran,

CAn you retry again by having the start date in 20 minutes and then have a schedule script to run daily.

Then for every min, you get the logs which prints current date and time and start date/time. Keep checking and when they are both same,then check if it is entering the if condition or not.

If it entered if condition, then you get another log, which says  "Entered into the if condition".

Then the event will be triggered. If you get the message under if and event is not triggered, then there could be issue with your event name.

If you are doing this in your PDI, share the pdi instance and credentials with me and we can check and fix the issue.

Mark the comment as helpful if it helps to debug.

Hi Asif.. Thanks for your help.. I just created a new instance.  https://dev70562.service-now.com/

ID: Admin

Pass: Isco@123

Schedule name : Send change email remainder

Notification: 15 Mins Change reminder

 

okay, i am looking into it.

Hi Imran,

I have corrected your scheduled item. Since you are fetching all change requests and many change requests have start date empty and hence it was giving error when you performing addSeconds on empty string.

Also, corrected the code slightly. Checked with 1 change request by having the time in 15 minutes and ran and its workign fine. I can see the event and notification both got triggered successfully.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Thanks a lot Asif. It worked perfectly. 🙂 Am trying to understand the code. And one final question, do we need to run the schedule at every 1 min interval? Does it affect the performance? Can we increase the interval time?