add Duration to Date/Time

jeremyphillips
Kilo Contributor

Howdy--

I have a record that has the following fields:

u_start_time (Date/Time)

u_end_time (Date/Time)

u_activity_duration (Duration)

In a Business Rule, I am trying to add u_activity_duration to u_start_time to calculate u_end_time.

I have tried the following:

(function executeRule(current, previous /*null when async*/) {

      // calculates the end time of an activity based on the start time and duration

  gs.log("Start time: " + current.u_start_time);

  gs.log("Duration: " + current.u_activity_duration);

  gs.log("Calculated end time: " + current.u_start_time.add(current.u_activity_duration));

  current.u_end_time = current.u_start_time.add(current.u_activity_duration);

})(current, previous);

That results in the following logs:

Start time: 2016-07-11 15:23:48

Duration: 1970-01-01 00:59:00

Calculated end time: undefined

After searching these forums, I have tried many variations on the script above, including using getValue, getNumericValue, etc., all with the same results.

Any suggestions on how I can add the Start Time and Duration to calculate and End Time?

Thanks,

Jeremy.

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here you go



var gdt = new GlideDateTime(current.u_start_date.getDisplayValue());


var ms=current.u_activity_duration.dateNumericValue();


gdt.add(ms);


current.end_date=gdt.getValue();


View solution in original post

11 REPLIES 11

Chuck Tomasi
Tera Patron

Hi Jeremy,



You'll need to use the getDisplayValue() method to show the duration field in "human terms". That aside, see the following link for how to add values to a duration.



You may also want to see the GlideDateTime method (available on the same page from the left.)




ServiceNow Developers


Unable to see the page ... Can you let me know how to add the durations (time worked) filed of two incident and then insert in some other table ?

check this out , am adding the previous duration value to a new duration calculation for processes that come loop around multiple times. This will let you add up the cumulate time spent in each process step.  So my question to you is if you you guys created DurationCalculator(); and get.DurationValue() why not build an api that does aspects of time in one step..... and put it in Flow Designer (click and drag). You have no idea how may articles and videos I went through to work this out. 

 

(function executeRule(current, previous /*null when async*/) {

var hold = current.u_triage_assign_duration.dateNumericValue() ;
          gs.addInfoMessage("hold =" +hold);
var start = current.u_triage_assign_d_t.getDisplayValue() ;
var end = current.u_triage_assign_end.getDisplayValue() ;
          gs.addInfoMessage("start =" +start);
          gs.addInfoMessage("end =" +end);

var dc = new DurationCalculator();
var durationInSeconds = dc.calcScheduleDuration(start,end); //Returns seconds
          gs.addInfoMessage("durationInSeconds =" +durationInSeconds);
var durationInMs = (durationInSeconds * 1000)+ hold;
          gs.addInfoMessage("durationInMs + hold =" +durationInMs);

var gDuration = new GlideDuration(durationInMs ); //Takes MS as paramater
         gs.addInfoMessage("gDuration =" +gDuration.getDurationValue());

current.u_triage_assign_duration= gDuration.getDurationValue();


})(current, previous);

Abhinay Erra
Giga Sage

Here you go



var gdt = new GlideDateTime(current.u_start_date.getDisplayValue());


var ms=current.u_activity_duration.dateNumericValue();


gdt.add(ms);


current.end_date=gdt.getValue();