Calculate duration based on schedule

palmen
Tera Guru

I have a requirement I'm working on but don't get it to work.

I'm running a scheduled script to see if 3 working days has past since a known date and time. If more then 3 days (24h of work time) has passed I should set a checkbox to true.

The schedule I'm using is from 8am to 4pm each day, total of 8h.

This is what I've been trying but there is something wrong with the duration calculation, I also need help on how to see if more then 24h of work time has passed.

I never see "gs.log('DURATION: ' + dur);" in the sys log, but I see the number in the log.

var test = new GlideRecord('u_rekrytering');

test.addQuery('number', 'REKR0000006');

test.query();

while(test.next()){

  gs.log('---THIS IS LOG FOR ' + test.number + '---');

  var schedule = new GlideSchedule('4478d9d969bfa100f336d9fc410262ec');

  var dur = schedule.duration(test.u_start, gs.nowDateTime());

  gs.log('DURATION: ' + dur);

  if (dur >= ---More than 24h work hours---) {

  test.u_awaiting_assignee = 'true';

  test.update();

  }

}

1 ACCEPTED SOLUTION

I've got it to work now, instead of the previous solution I'm using DurationCalculator() which seems to be the easiest way to calculate the duration based on a schedule.



var ser = new GlideRecord('u_rekrytering');


ser.addQuery('number', 'REKR0000070');


ser.query();


while(ser.next()){


  var dc = new DurationCalculator();


  dc.setSchedule('4478d9d969bfa100f336d9fc410262ec');


  var dur = dc.calcScheduleDuration(ser.u_planned_end_time, gs.nowDateTime());


  if (dur > 86400) {


      //86400 sec = 24h = 3 days according to schedule


      ser.u_awaiting_assignee = 'true';


      ser.update();


  }


}


View solution in original post

7 REPLIES 7

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Tja,



When we are calculation we have put the .getGlideObject() as well. Not really sure, I didnt do this script. But here is our example:


It's from a metric which sets "breached" if the time is over 1 hour,   but I think you get it 😃



function createMetric() {


  var mi = new MetricInstance(definition, current);


  if (mi.metricExists())


  return;



  var gr = mi.getNewRecord();


  var sched = new GlideSchedule('f79ad0960fad3500aa7c7f5ce1050eb9');


  gr.start = current.opened_at;


  gr.end = current.sys_updated_on;


  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());


  var dur = sched.duration(gr.start.getGlideObject(), gr.end.getGlideObject());


  gr.business_duration = dur;


  var durNum = gr.business_duration.getGlideObject().getNumericValue();


  var durationSeconds = (durNum/1000);


  if(durationSeconds > 3600){


  gr.value = "breached";


  }


  else


  {


  gr.value = "not breached";


  }


  gr.calculation_complete = true;


  gr.insert();


}


Thanks for the help. I've almost got it to work now.


If I calculate duration between 2 different date/time fields it's working, but I want to calculate the duration until the current time (gs.nowDateTime()) and that's not working.



This script is working calculating duration between 2 date/time fields


var ser = new GlideRecord('u_rekrytering');


ser.addQuery('number', 'REKR0000070');


ser.query();


while(ser.next()){


  var sched = new GlideSchedule('4478d9d969bfa100f336d9fc410262ec');


  var dur = sched.duration(ser.u_planned_end_time.getGlideObject(), ser.u_stop_publishing.getGlideObject());


  var durNum = dur.getNumericValue();


  var durationSeconds = (durNum/1000);


  if (durationSeconds > 86400) { //86400 sec = 24h = 3 days


  ser.u_awaiting_assignee = 'true';


  ser.update();


  }


}



When I try to calculation duration until the current time it's not working, how can I solve it?


gs.nowDateTime().getGlideObject() is returning the current time.


var ser = new GlideRecord('u_rekrytering');


ser.addQuery('number', 'REKR0000070');


ser.query();


while(ser.next()){


  var sched = new GlideSchedule('4478d9d969bfa100f336d9fc410262ec');


  var dur = sched.duration(ser.u_planned_end_time.getGlideObject(), gs.nowDateTime().getGlideObject());


  var durNum = dur.getNumericValue();


  var durationSeconds = (durNum/1000);


  if (durationSeconds > 86400) { //86400 sec = 24h = 3 days


  ser.u_awaiting_assignee = 'true';


  ser.update();


  }


}


Do you get any value in the durNum?



Don't know how it will handle it if _planeed_end_time is in the future. since the examples the order is duration(start time, end time).



Perhaps you can use dateDiff instead?



Setting the Duration Field Value - ServiceNow Wiki


I don't get any value for dur when trying to use nowDateTime(), so there is something wrong with sched.duration() when trying to use gs.nowDateTime()


I'm not going to use a field that will be in the future, only using planned end time because that was a field I had populated on the form when starting this script.



I've been looking at using dateDiff, but haven't found out how I can use it with a schedule.