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

@suuriyas Did you debug your script?
Add log message and see what it's returning.
I just tried in my PDi, it's working..

 

suuriyas
Tera Contributor

HI @J Siva ,

 

Yeah i tried but it did not work so went with other method.

Made BR as inactive and in dictionary override i added the calculation like this it works but even when the resolved state is empty some value is getting store in duration field like 11 hours something like that.

i have submitted a new request and when i see in table opened field is showing value state is new and no value in resolved field as expected buy in duration some random value is getting displayed like 11 hours and once i make it resloved now it is calculating correctly any reason why

suuriyas_0-1761828017547.pngsuuriyas_1-1761828069825.pngsuuriyas_2-1761828097021.png

 

solution:

if (!current.opened_at.nil() && !current.resolved_at.nil()){
var opened = new GlideDateTime(current.getValue('opened_at'));
var resolved = new GlideDateTime(current.getValue('resolved_at'));

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

@suuriyas 

Just a tip..

You can replace the below with the business rule filter condition 

current.opened_at.nil() && !current.resolved_at.nil()

Ankur Bawiskar
Tera Patron
Tera Patron

@suuriyas 

please use script shared by @J Siva 

Debug and see, it should work ideally

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader