Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Calculating a duration from 2 date / time fields in a scoped application

travis_warner
Giga Contributor

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.

Screen Shot 2016-11-22 at 9.55.44 AM.png

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();

}

1 ACCEPTED SOLUTION

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.


Screen Shot 2016-11-22 at 10.21.13 AM.png


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();


}


View solution in original post

7 REPLIES 7

Chuck Tomasi
Tera Patron

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


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.


Screen Shot 2016-11-22 at 10.21.13 AM.png


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();


}


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());


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? 


find_real_file.png

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.

find_real_file.png

I In my script, I tried to use the correct answer here on your forum.

 

Your response will be much appreciated, Thanks in advance!