Date difference

sigmachiuta
Kilo Guru

I am trying to run a script when the time is within 1 minute of the due date time.   How can I make sure that I am comparing the correct values here?   I tried to run a background script that would show me the values to see why the script below is triggering almost immediately, but it returned null.

var gr= new GlideRecord('incident')

gr.addQuery('active', true);

gr.addQuery('number', 'INC00051753')

gr.query();

gs.print("date diff: " + gs.dateDiff(gr.due_date,gs.nowDateTime(), false));

var gr = new GlideRecord('incident');

gr.addQuery('active', true);

gr.addQuery('state', 'IN', '2,3,4' );

gr.query();

while(gr.next()){

var time = gs.dateDiff(gr.due_date.getDisplayValue(),gs.nowDateTime(),true);

time = parseFloat(time);

//set state

if( time <= 60) {

//set state

gr.state = 1;

gr.update();

}

}

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Right. dateDiff is a shortcut for a GlideDuration calculation:



GlideDuration gd = new GlideDuration(end.getNumericValue() - start.getNumericValue());



Depending on wether you want a numeric value or not, there is some conversion to a number then a string, but basically it's just "what is the amount of time I'd have to *add* to the start date to get to the end date". If your start date is in the past and your end date in the future, then you get a positive number. If you reverse them, you get a negative number (because you's have add a negative amount of time to the start date to get 'back' to the end date).



If you just want the *absolute value*, run the returned value through Math.abs():



var gr= new GlideRecord('incident');


gr.addQuery('active', true);


gr.addQuery('number', 'INC00051753');


gr.query();



if (gr.next())


      gs.print("date diff: " + Math.abs(gs.dateDiff(gr.getValue('due_date'), gs.nowNoTZ(), true)));


View solution in original post

11 REPLIES 11

Thijs Daemen
Mega Guru

Hi Sigmachiuta,



In order for something to run 1 minute before the due date passes, you will also need a scheduled job with a very low timer. Be sure to limit your query on the table correctly in order to prevent running it on every record in the table every 45 seconds.



something like (this is similar to how ServiceNow triggers the calculations for SLA's with scheduled jobs):



var end = gs.minutesAgo(-1);


var gr = new GlideRecord('incident');


gr.addQuery('due_date', '>', gs.nowDateTime())


gr.addQuery('due_date', '<', end);



I'm not sure running a query on the table every 30 to 45 seconds is a good idea. It might be wise to look at a higher timer for your requirement.


coryseering
ServiceNow Employee
ServiceNow Employee

Right. dateDiff is a shortcut for a GlideDuration calculation:



GlideDuration gd = new GlideDuration(end.getNumericValue() - start.getNumericValue());



Depending on wether you want a numeric value or not, there is some conversion to a number then a string, but basically it's just "what is the amount of time I'd have to *add* to the start date to get to the end date". If your start date is in the past and your end date in the future, then you get a positive number. If you reverse them, you get a negative number (because you's have add a negative amount of time to the start date to get 'back' to the end date).



If you just want the *absolute value*, run the returned value through Math.abs():



var gr= new GlideRecord('incident');


gr.addQuery('active', true);


gr.addQuery('number', 'INC00051753');


gr.query();



if (gr.next())


      gs.print("date diff: " + Math.abs(gs.dateDiff(gr.getValue('due_date'), gs.nowNoTZ(), true)));