Issue Parsing getDurationValue() Equalling 5 Days or More

Wasdom_Kung
Tera Guru

Hello, 

I have a script that shows a widget based on the duration a ticket has been open, it's priority and some other factors. Currently, when a new ticket is logged, the widget should not show, however when going through the script debugger, 120:00:00 always evaluates to true, despite the ticket ages being minutes old, could someone explain this?

I have previous methods before this that execute properly, such as 04:00:00, 24:00:00 and 72:00:00.

 

OpenTimeMS is calculated by:

 

// Get length of time ticket has been open in days, hours, minutes, seconds
var diff2 = GlideDateTime.subtract(new GlideDateTime(gr.sys_created_on), new GlideDateTime());
var timeOpenMS = diff2.getDurationValue();

 

 

 

 

// Incidents open for 5 days or longer, escalation is 0, priority is 1, 2, 3 or 4
// State is new or work in progress, escalation 0, priority is 1, 2, 3 or 4
if (timeOpenMS > "120:00:00" && data.table == 'incident' && gr.active == true && gr.priority == 1 || gr.priority == 2 || gr.priority == 3 || gr.priority == 4 && gr.incident_state == 1 || gr.incident_state == 2 && gr.escalation == 0 && gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showEscalate = true;
        vEscalate = 0;
    }

 

 

 


Thanks!

1 ACCEPTED SOLUTION

Fixed with the following example, it seems that by retrieved time somehow reverted back to D HH:MM:SS format but now works as expected:

 

// Get length of time ticket has been open in days, hours, minutes, seconds
var diff2 = GlideDateTime.subtract(new GlideDateTime(gr.sys_created_on), new GlideDateTime());
var timeOpenMS = diff2.getDurationValue();
// Runs when a ticket is viewed from the service protal > my tickets > incident
gs.info("The duration of this ticket is: " + diff2.getDurationValue()); //syslog_list.do

 

 

Example conditions:

 

// Set escalation widget to false by default
if (timeOpenMS < "04:00:00" && data.table == 'incident' && gr.active == true && gr.escalation == 0 && gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' &&
            (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
            data.showWidget = false;
            data.showEscalate = false;
}
 if (timeOpenMS > "5 00:00:00" && data.table == 'incident' && gr.active == true &&
                (gr.priority == 1 || gr.priority == 2 || gr.priority == 3 || gr.priority == 4) &&
                (gr.incident_state == 1 || gr.incident_state == 2) && gr.escalation == 0 &&
                gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' &&
                (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
                data.showWidget = true;
                data.showEscalate = true;
                vEscalate = 0;
}

 

View solution in original post

3 REPLIES 3

Ratnakar7
Mega Sage
Mega Sage

Hi @Wasdom_Kung ,

 

You are comparing the duration value as a string. To properly compare the duration, you can convert it to a numeric value.

Here's an updated version of the comparison:

var timeOpen = new GlideDuration(timeOpenMS);
var timeOpenValue = timeOpen.getNumericValue();

// Compare timeOpenValue to the threshold
if (timeOpenValue > 120 && data.table == 'incident' && gr.active == true &&
    (gr.priority == 1 || gr.priority == 2 || gr.priority == 3 || gr.priority == 4) &&
    (gr.incident_state == 1 || gr.incident_state == 2) && gr.escalation == 0 &&
    gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' &&
    (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
    data.showWidget = true;
    data.showEscalate = true;
    vEscalate = 0;
}

 

 

Thanks,

Ratnakar

Hi Ratnakar,

I've made the following changes as you suggested, it looks like it works but for this example ticket, that has been open for 24 hours, still evaluates true in debugging at 120 (5 days) of being open hmmm, it successfully evaluates as false for the 4, 24 and 72 statements.

Changes:

 

 

// Get length of time ticket has been open in days, hours, minutes, seconds
var diff2 = GlideDateTime.subtract(new GlideDateTime(gr.sys_created_on), new GlideDateTime());
var timeOpenMS = diff2.getDurationValue();
	
var timeOpen = new GlideDuration(timeOpenMS);
var timeOpenValue = timeOpen.getNumericValue();

// Incidents open for 5 days or longer, escalation is 0, priority is 1, 2, 3 or 4
// State is new or work in progress, escalation 0, priority is 1, 2, 3 or 4
if (timeOpenValue > 120 && data.table == 'incident' && gr.active == true && gr.priority == 1 || gr.priority == 2 || gr.priority == 3 || gr.priority == 4 && gr.incident_state == 1 || gr.incident_state == 2 && gr.escalation == 0 && gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' && (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
        data.showWidget = true;
        data.showEscalate = true;
        vEscalate = 0;
}

 

 

 

Edit:
Upon checking sys_log it looks like it's because the duration now shows as "The duration of this ticket is: 69367000" which is 0.8 days, so I don't think the conversion for timeOpenMS to timeOpenValue is working correctly.

I output the value of diff2 before any conversion and this is the value returned, I believe this is causing the issue?
 1970-01-01 20:38:28

Edit 2:
Actually didn't notice some other syntax changes in your provided code, I've modified 120 to 5 days in milliseconds which is 
432000000 and tested and it has worked now, thanks!

Fixed with the following example, it seems that by retrieved time somehow reverted back to D HH:MM:SS format but now works as expected:

 

// Get length of time ticket has been open in days, hours, minutes, seconds
var diff2 = GlideDateTime.subtract(new GlideDateTime(gr.sys_created_on), new GlideDateTime());
var timeOpenMS = diff2.getDurationValue();
// Runs when a ticket is viewed from the service protal > my tickets > incident
gs.info("The duration of this ticket is: " + diff2.getDurationValue()); //syslog_list.do

 

 

Example conditions:

 

// Set escalation widget to false by default
if (timeOpenMS < "04:00:00" && data.table == 'incident' && gr.active == true && gr.escalation == 0 && gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' &&
            (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
            data.showWidget = false;
            data.showEscalate = false;
}
 if (timeOpenMS > "5 00:00:00" && data.table == 'incident' && gr.active == true &&
                (gr.priority == 1 || gr.priority == 2 || gr.priority == 3 || gr.priority == 4) &&
                (gr.incident_state == 1 || gr.incident_state == 2) && gr.escalation == 0 &&
                gr.company == 'ad0459981b7c0c100375ca217e4bcbb0' &&
                (gr.caller_id == gs.getUserID() || gs.hasRole("admin"))) {
                data.showWidget = true;
                data.showEscalate = true;
                vEscalate = 0;
}