How can i modify below Script

Ankit Kumar6
Tera Contributor

Hi developers,

How can exclude the time frame when the ticket is in hold state(3) in the below script.

please help me how can i modify the below script.

 

var s = current.incident_state;
if (s >= 6)
createMetric();
function createMetric() {
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
if(current.sys_updated_on=='')
{
gr.end=gs.nowDateTime();
}
else
{
gr.end = current.sys_updated_on;
}
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
3 REPLIES 3

Vishwa Pandya19
Mega Sage

Hi Ankit,

 

Assuming your script is working you can add below change to the condition.

var s = current.incident_state;
if (s >= 6 && s!=3)  //state is greater than or equal to 6 and state is not 3
createMetric();
function createMetric() {
var mi = new MetricInstance(definition, current);
if (mi.metricExists())
return;
var gr = mi.getNewRecord();
gr.start = current.sys_created_on;
if(current.sys_updated_on=='')
{
gr.end=gs.nowDateTime();
}
else
{
gr.end = current.sys_updated_on;
}
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());

 This will make sure that the calculation only works on other states.


If my answer helped you in any way please mark it as correct or helpful. 

@Vishwa Pandya19 

if (s >= 6 && s!=3)

This line only check state is not in hold.

suppose my  ticket is in hold for 4hrs. then how can i subtract this time from total time to resolve a ticket.

Maddysunil
Kilo Sage

@Ankit Kumar6 

Please try with below code

 

var s = current.incident_state;

// Check if the ticket is resolved or closed and not in the "On Hold" state
if (s >= 6 && s != 3) {
    createMetric();
}

function createMetric() {
    var mi = new MetricInstance(definition, current);
    if (mi.metricExists()) {
        return;
    }

    var gr = mi.getNewRecord();
    gr.start = current.sys_created_on;
    
    // Calculate the duration excluding time spent in the "On Hold" state
    var durationExcludingHold = calculateDurationExcludingHold(current);

    // If the ticket is still open, use current time as the end time
    if (current.active == true) {
        gr.end = gs.nowDateTime();
    } else {
        // If the ticket is closed, use the closed time as the end time
        gr.end = current.sys_updated_on;
    }

    gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue()) - durationExcludingHold;
}

// Function to calculate the total time spent in the "On Hold" state
function calculateDurationExcludingHold(current) {
    var holdTime = 0;
    var gr = new GlideRecord('incident_state'); // Assuming 'incident_state' is the table where state transitions are stored
    gr.addQuery('incident', current.sys_id); // Filter by the current incident
    gr.addQuery('state', 3); // Filter by the "On Hold" state
    gr.query();
    while (gr.next()) {
        var holdStart = gr.sys_created_on;
        var holdEnd = gr.sys_updated_on || gs.nowDateTime(); // If the state is still active, use current time as end time
        holdTime += gs.dateDiff(holdStart.getDisplayValue(), holdEnd.getDisplayValue());
    }
    return holdTime;
}

 

  

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks