Metric Definition - Script Calculation on RITM Stage Field

Mike Cory
Tera Contributor

I have a Catalog Item (Firewall Rule Change) that generates a Change Request after the RITM is approved.  The RITM is set to Closed Complete after the Approval activity completes.

There is a new requirement to capture the Duration of when the Request is opened (Waiting for Approval) to when it gets approved (Complete).

I created a new Metric Definition that runs on the Requested Item (sc_req_item) table.  When I set the Type as "Field value duration", it sets the duration correctly.  It only calculates the difference between created on and closed, which is fine because that essentially is what I need.

 

However, I only want the Metric to capture records if the Catalog Item = Firewall Rule Change, so I have to set the Type to “Script calculation” and use the Script.  This is where I’m struggling.

 

The stage transitions through waiting for approval to fulfillment, to complete. I would like to get the Duration of when the RITM is created (Stage = waiting_for_approval) to when it is completed (Stage = complete).  My script is not working.    Any help appreciated.

 

MikeCory_0-1709313137141.png

 

 

 

1 ACCEPTED SOLUTION

Manoj89
Giga Sage

Hi Cory,

 

I would do something like this

 

// definiton: GlideRecord - this row
// current: current record
if (current.sc_cat_item == 'firewall') {
    if (current.stage== 'requested')
        createMd();
    else if(current.stage== 'approved')
        closeMd();
}

//calls the script include "MetricInstance" and create a metric instance
function createMd() {
    var md = new MetricInstance(definition, current);

    // 	check if a metric instance exists already
    if (md.metricExists()) {
        return;
    }

    var gr = md.startDuration();
}

//calls the script include "MetricInstance" and close the metric instance
function closeMd() {
    var md = new MetricInstance(definition, current);

    if (md.metricExists()) {
        md.endDuration();
    }
}

 




View solution in original post

5 REPLIES 5

Anubhav24
Mega Sage
Mega Sage

Hi @Mike Cory ,

Assuming you have problems in date difference calculation.

The sys_created_on field is of data type datetime and gs.now returns only date i.e. 2024-03-01 , so before doing the calculation you must convert either start date to GlideDate format or end to GlideDateTime.

Instead of gs.now you can use gs.nowDateTime() , after making both dates to same data type,  you can try the datediff function if that does not works you need to make use of getnumericvalue and setnumericvalue functions as below:

nDate1 = date1.getNumericValue();
nDate2 = date2.getNumericValue();
var ndiff = nDate2-nDate1;

var gdt = new GlideDateTime();
gdt.setNumericValue(ndiff);

 

Please mark correct/helpful if my response helped you.

Made the changes to the script (below). It is still not working.   Just like before it doesn't even create the Metric record after the RITM is created.

 

var stg = current.stage;
if ((current.cat_item.name == "Firewall Rule Change")){
    createMetric();
}
function createMetric() {
  var mi = new MetricInstance(definition, current);
  //if (mi.metricExists())
    //return;
  var gr = mi.getNewRecord();
  gr.field_value = stg;
  gr.start = current.sys_created_on;
  if (stg == waiting_for_approval || complete){
  gr.end = gs.nowDateTime();
  var nDate1 = start.getNumericValue();
  var nDate2 = end.getNumericValue();
  var ndiff = nDate2-nDate1;
  var gdt = new GlideDateTime();
  gdt.setNumericValue(ndiff);
  gr.duration = ndiff;
  }
  //gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
  gr.calculation_complete = true;
  gr.insert();

Hi @Mike Cory ,

Are the debug messages getting logged in your script , is the control coming to your script or not , if yes then can you try and print the values of ndate1 , ndate2 and ndiff after setnumericvalue statement and then please verify the data type of "duration" column as well.

 

Please mark correct/helpful if my response helped you.

Manoj89
Giga Sage

Hi Cory,

 

I would do something like this

 

// definiton: GlideRecord - this row
// current: current record
if (current.sc_cat_item == 'firewall') {
    if (current.stage== 'requested')
        createMd();
    else if(current.stage== 'approved')
        closeMd();
}

//calls the script include "MetricInstance" and create a metric instance
function createMd() {
    var md = new MetricInstance(definition, current);

    // 	check if a metric instance exists already
    if (md.metricExists()) {
        return;
    }

    var gr = md.startDuration();
}

//calls the script include "MetricInstance" and close the metric instance
function closeMd() {
    var md = new MetricInstance(definition, current);

    if (md.metricExists()) {
        md.endDuration();
    }
}