Record is not getting create on metrics instance

lucky24
Tera Contributor

Hi Team,
we have a checkbox (u_escalation) on the incident table.

When a user makes it true and then makes it false, we have to calculate the time between that.

For that, I have written scripted metrics, I know we can achieve through this form field value duration but I have to add one more requirement like I have to exclude the SLA breach time which stored in the task_sla table

 

// Check if u_escalation has changed to true
if (current.u_escalation.changesTo(true)) {
    createMetric(); // 
}
function createMetric() {
    var mi = new MetricInstance(definition, current);

    // Check if a metric instance already exists for this record
    if (mi.metricExists()) {
        return;
    }
    var gr = mi.getNewRecord();

    // Set the start time of the metric instance to when u_escalation was set to true
    gr.start = current.sys_updated_on; 

    // Check if u_escalation has changed to false
    if (current.u_escalation.changesTo(false)) {
        // Set the end time of the metric instance to when u_escalation was set to false
        gr.end = current.sys_updated_on;
        gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
        gr.calculation_complete = true;
        gr.insert();
    }
}

 

 In the metrics instance, no record is getting created, Can someone guide me to where I am making a mistake?

 

Thanks!

2 REPLIES 2

Mark Manders
Mega Patron

I don't see anything in your script about the SLA Breach time you mention, so I have no clue why you don't just use field value duration on that field. It will do it for you, without any scripting.

Your remark about SLA Breach time also isn't clear. What is it you are trying to achieve? Why do you need to exclude what? An escalation is an escalation, right? It shouldn't matter if it's within SLA or not.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi Mark,

Thanks for the response.

As per requirement, we need to exclude the SLA breach time right now I haven't added this logic in my script because I am getting issues that the record is not being created in the metrics instance.

Regarding exclude breach SLA time - I will glide record task_sla table and query to the has breached is true and will exclude breach time from end time that's I am thinking  but first thing I should able to create record in metrics instance

Thanks!