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

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

Hi Asif.. Thanks for your reply. I will try with your first approach and get back.

Hello Imran,

Kindly accept the comment as a correct answer if this has helped to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Hi Asif..

 

The script which you provided is not triggering the event. 

 

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, "", "");
  }
}

 

I tried the below script, but it shows all the changes for today not only the ones which is going to trigger in 15 mins time.

 

var gr = new GlideRecord('change_request');


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


gr.addEncodedQuery('active=true^start_dateRELATIVEGT@minute@ago@15^start_dateONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');

 

gr.query();


while(gr.next()) {


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


}

Hello Imran,

i added few logs. can you check once again and share the logs output.

Also, please note that you need to make your script to run every min for the script to work.

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);
gs.log("Start date and time is "+gdt);
var gdt1 = new GlideDateTime();
gs.log("Current date and time is "+gdt1);
  if(gdt.equals(gdt1)) {
    gs.log("Entered into the if condition");
    gs.eventQueue("change.remainder", gr, "", "");
  }
}