Set the value of the metric duration field to the field created in the Defects table

Miguel1
Tera Contributor

Hi Experts!!
I made a Metric definition, Type: Script calculation, in Defect [u_defect] table.

This script monitors the status change of the defect from when it is opened until it goes to Inspection. The code works fine but I would like to know how I can take the duration value that the metric does and put it in another duration field inside the defect table.

Any ideas please?

add Image and link below:

find_real_file.png

find_real_file.png

var s = current.state;
if (s == 2) //2 == Inspection
  createMetric();

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

  var gr = mi.getNewRecord();
  gr.start = current.opened_at;
  gr.end = current.sys_updated_on;
  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.calculation_complete = true;
  gr.insert();
}

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

Add the update action to the metric:

var s = current.state;
if (s == 2) //2 == Inspection
    createMetric();

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

    var gr = mi.getNewRecord();
    gr.start = current.opened_at;
    gr.end = current.sys_updated_on;
    gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
    gr.calculation_complete = true;
    gr.insert();
    var grCurrent = new GlideRecord("incident");
    if (grCurrent.get(current.sys_id)) {
        grCurrent. < DURATION FIELD > = gr.duration;
        grCurrent.update();
    }
}

Replace <DURATION FIELD>  with the duration field you want to fill and "incident" with the table you want.

View solution in original post

2 REPLIES 2

Willem
Giga Sage
Giga Sage

Add the update action to the metric:

var s = current.state;
if (s == 2) //2 == Inspection
    createMetric();

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

    var gr = mi.getNewRecord();
    gr.start = current.opened_at;
    gr.end = current.sys_updated_on;
    gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
    gr.calculation_complete = true;
    gr.insert();
    var grCurrent = new GlideRecord("incident");
    if (grCurrent.get(current.sys_id)) {
        grCurrent. < DURATION FIELD > = gr.duration;
        grCurrent.update();
    }
}

Replace <DURATION FIELD>  with the duration field you want to fill and "incident" with the table you want.

Miguel1
Tera Contributor

Thanks a lot Willem it works