- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2016 06:56 AM
I am trying to use a business rule to auto calculate a duration field in that is used in a scoped application. I need the duration based on the difference of 2 date / time fields I tried to modify one that we are already using for Problem management but the gs.dateDiff is only available in global. Below is a copy of the current script being used as well as the error im currently getting.
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
gdt1 = current.u_incident_start.getGlideObject();
gdt2 = current.u_resolution_time.getGlideObject();
current.u_mttr = GlideDateTime.subtract(gdt1.getDisplayValue(),gdt2.getDisplayValue());
current.update();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2016 07:28 AM
Thanks Chuck, that got me close. I had to adjust to the following to make it work but I may have missed something in your original post.
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
current.u_mttr = GlideDateTime.subtract(new GlideDateTime(current.getValue("u_incident_start")), new GlideDateTime(current.getValue("u_resolution_time")));
current.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2016 07:01 AM
The date/time fields are already glide element objects. You don't need to use the getGlideObject. I'm going to assume your u_mttr is a duration field as well. If that's the case, then this is all you need.
current.u_mttr = GlideDateTime.subtract(current.u_incident_start, current.u_resolution_time);
gs.debug(dur.getDisplayValue()); // show the resulting difference for debugging
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2016 07:28 AM
Thanks Chuck, that got me close. I had to adjust to the following to make it work but I may have missed something in your original post.
function onBefore(current, previous) {
//This function will be automatically called when this rule is processed.
current.u_mttr = GlideDateTime.subtract(new GlideDateTime(current.getValue("u_incident_start")), new GlideDateTime(current.getValue("u_resolution_time")));
current.update();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2016 07:44 AM
Here's a test I ran from scripts background that appears to work just fine. Update with a current.update() accordingly at the end.
var gdt1 = new GlideDateTime(current.opened_at.getDisplayValue());
var gdt2 = new GlideDateTime(current.resolved_at.getDisplayValue());
var dur = new GlideDuration();
var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
gs.print(dur.getDisplayValue());
current.u_mttr = dur;
gs.debug(current.u_mttr.getDisplayValue());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2020 09:28 AM
Hi Travis,
I have the same requirement I need to calculate two date/time fields and display the answer to a custom field.
Is this a business rule? If so, what condition did you use to make this work?
This is what I set on my business rule? Am I doing it right?
I used After Update, because the requirement is to populate the custom field by the difference of the two date/time fields after the ticket is closed.
I In my script, I tried to use the correct answer here on your forum.
Your response will be much appreciated, Thanks in advance!