- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2016 03:33 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2016 06:56 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2016 06:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2016 06:57 AM
What does the log show? Does it show correct value?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2016 07:00 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2016 06:56 AM
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);