Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Subtract time from duration

palmen
Tera Guru

I've got a duration field and wan't to substract a specified time from this duration. How do I do this in a Business Rule?

I've got it to work when trying to add the time, but not when subtracting.

This is the code I'm working with at the moment.

calendar_duration is the OOTB field on incident

u_correction_time is a glide_time field

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

  //Get the Duration

  var dur = new GlideDuration();

  dur.setValue(current.calendar_duration);

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

  //Get the Correction time and calculate adjusted duration

  var corr = new GlideDuration();

  corr.setValue(current.u_correction_time);

  gs.log('CORRECTION: ' + corr);

  dur.subtract(corr);

  //Set Adjusted Duration to dur variable

  current.u_adjusted_duration = dur;

})(current, previous);

The adjusted duration is wrong with 1h as you can see here, the Adjusted Duration should have been 2 hours in this case

find_real_file.png

1 ACCEPTED SOLUTION

palmen
Tera Guru

Think I've solved it now, had to add/remove the offset for the user compared to GMT to get the correct values



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



  //Get the Duration


  var dur = new GlideDateTime(current.calendar_duration);



  //Get the Correction time (this time will be adjusted to the users timezone)


  var corr = new GlideDuration();


  corr.setValue(current.u_correction_time);



  //Get the local time of the user


  var gdt = new GlideDateTime();


  gdt.getLocalTime();



  //Add/remove time from correction based on the offset of user local time


  corr.add(gdt.getTZOffset());



  //Subtract the correction time from the duration


  dur.subtract(corr);



  //Set Adjusted Duration to dur variable


  current.u_adjusted_duration = dur;



})(current, previous);


View solution in original post

4 REPLIES 4

palmen
Tera Guru

The issue is that the correction time in the example is 4 hours, but the value I get is 3h stored in the corr variable.


How can I get the correct value stored in corr?


It always subtract 1h from the time shown on the form, therefore I get the adjusted duration calculated wrong.



The value stored in the corr variable adjust to my timezone. If I change the timezone to be GMT+2 it removed 2h from the value in "Correction time" field


What does the log show? Does it show correct value?


The log for corr variable showed 1h less compared to the form.


This was du to timezone since my user is in CET timezone and not in GMT.


I should have fixed it now with the post I marked as correct


palmen
Tera Guru

Think I've solved it now, had to add/remove the offset for the user compared to GMT to get the correct values



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



  //Get the Duration


  var dur = new GlideDateTime(current.calendar_duration);



  //Get the Correction time (this time will be adjusted to the users timezone)


  var corr = new GlideDuration();


  corr.setValue(current.u_correction_time);



  //Get the local time of the user


  var gdt = new GlideDateTime();


  gdt.getLocalTime();



  //Add/remove time from correction based on the offset of user local time


  corr.add(gdt.getTZOffset());



  //Subtract the correction time from the duration


  dur.subtract(corr);



  //Set Adjusted Duration to dur variable


  current.u_adjusted_duration = dur;



})(current, previous);