Duration field should contain the time from the time opened to resolved.

suuriyas
Tera Contributor

HI Community,

 

I have a requirement, in scoped application table called K case in this table we have field called opened at and resolved at where it show date and time of the case opened and resolved and there is field called duration.

In this duration field i need to populate the value with time from opened to reloved.

 

i tried using BR and also used dictionary override with override calculation but both are not working.

and in calculation part .datediff is not valid it seems

 

my script:

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

    if (current.opened_at && current.state == '11'){
        var opened = new GlideDateTime(current.opened_at);
        var resolved = new GlideDateTime();

        var durationMS = resolved.getNumericValue() - opened.getNumericValue();
        var totalSeconds = Math.floor(durationMS/1000);
        var days = Math.floor(totalSeconds/(24 * 3600));
        totalSeconds %= (24*3600);
        var hours = Math.floor(totalSeconds/3600);
        totalSeconds%= 3600;
        var minutes =Math.floor(totalSeconds/60);
        var durationStr = '';
        if(days>0)durationStr += days + 'day'+(days>1?'s':'');
        if(hours>0)durationStr += hours + 'hour'+(hours>1?'s':'');
        if(minutes>0 || durationStr == '') durationStr += minutes +'min'+(minutes>1?'s':'');

        //var duration = new GlideDuration(durationMS);
        current.calendar_duration = durationStr.trim();
    }

})(current, previous);
 
let me know what is wrong with it
please provide the correct one
9 REPLIES 9

J Siva
Kilo Patron
Kilo Patron

Hi @suuriyas 
Try the below script..
It's working for me as expected..

(function executeRule(current, previous /*null when async*/ ) {
    var opened  = new GlideDateTime(current.getValue('opened_at'));
    var resolved  = new GlideDateTime();

    var duration = GlideDateTime.subtract(opened, resolved); //the difference between opened and resolved

     current.calendar_duration = duration.getDurationValue();

})(current, previous);

Also, try to perform the validation using the Business Rule condition builder instead of script evaluation

Sample:

 

JSiva_1-1761822636888.png

 

Regards,
Siva

suuriyas
Tera Contributor

HI @J Siva ,

Thanks for replying i tried the same by giving condition as resolved changes in BR before insert/update but it did not work

suuriyas_0-1761823992615.png

 duration field not populated

 

also tried like this

if (current.opened_at && current.resolved_at){
        var opened = new GlideDateTime(current.opened_at);
        var resolved = new GlideDateTime(current.resolved_at);
        var duration = GlideDateTime.subtract(resolved,opened);
        current.calendar_duration  = duration;
    }

@suuriyas 
 var duration = GlideDateTime.subtract(resolved,opened); =>  var duration = GlideDateTime.subtract(opened, resolved);

First parameter should be start date and the second one should be end date.

 

 current.calendar_duration  = duration; => current.calendar_duration = duration.getDurationValue();

 

suuriyas
Tera Contributor

HI @J Siva ,

 

Yeah i tried your script as well but it did not work for me

var opened  = new GlideDateTime(current.getValue('opened_at'));
    var resolved  = new GlideDateTime();

    var duration = GlideDateTime.subtract(opened, resolved); //the difference between opened and resolved

     current.calendar_duration = duration.getDurationValue();
 
not sure what is wrong