Compare dates in scheduled jobs

ceraulo
Mega Guru

Hello!

I have a scheduled job that triggers an event to send a notification. It should trigger when the due date is one day from today aka tomorrow. The script in my scheduled job is

var gr = new GlideRecord("sc_task");
gr.addEncodedQuery('active=true^short_descriptionLIKEFOR TESTING ONLY');
gr.query();
while (gr.next()) {

 var end_date = new GlideDateTime(gr.request_item.due_date.getDisplayValue());
 end_date.addDays(-1);
 var today_date = gs.nowDateTime();

  if (end_date == today_date) {
         gs.eventQueue('event_name', gr, gs.getUserID()); 
    }
}

Please help!

Thank you.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

you can also use encodedQuery

var gr = new GlideRecord("sc_task");
gr.addEncodedQuery('request_item.due_dateRELATIVELT@dayofweek@ahead@1');
gr.query();
while(gr.next()) {
   gs.eventQueue('event_name', gr, gs.getUserID()); 
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as below

var gr = new GlideRecord("sc_task");
gr.addEncodedQuery('active=true^short_descriptionLIKEFOR TESTING ONLY');
gr.query();
while (gr.next()) {

 var end_date = new GlideDateTime(gr.request_item.due_date);
 end_date.addDays(-1);
 var today_date = new GlideDateTime();

  if (end_date.getNumericValue() == today_date.getNumericValue()) {
         gs.eventQueue('event_name', gr, gs.getUserID()); 
    }
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

you can also use encodedQuery

var gr = new GlideRecord("sc_task");
gr.addEncodedQuery('request_item.due_dateRELATIVELT@dayofweek@ahead@1');
gr.query();
while(gr.next()) {
   gs.eventQueue('event_name', gr, gs.getUserID()); 
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

asifnoor
Kilo Patron

Hi,

If the due date (once set) will not change, then use gs.eventQueueScheduled which can help to trigger an event on a specified date/time.

You can write an after update BR on your table with filter conditions

DueDate changes

and add this code.

var end_date = new GlideDateTime(current.request_item.due_date);
end_date.addDays(-1); //take one day before due date
gs.eventQueueScheduled("your_event",current,"","",end_date);

If the due date going to be updated, then go with scheduled job. Do not use getDisplayVAlue() when getting the date.

Mark the comment as correct/helpful if this helps to solve the problem.

 

Also check out reminders feature of ServiceNow. This does not require any coding.

https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/task-table/concept/...

Mark the comment as correct/helpful if this helps to solve the problem.