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

Mike Cory
Tera Contributor
 

Thank you for the information.  Works great!  I just needed to change the stage values to what the workflow is setting them to, and it works great.  Modified script below.  Thanks!

 

// definiton: GlideRecord - this row
// current: current record
if ((current.cat_item.name == "Firewall Rule Change")){
    if(current.stage == 'waiting_for_approval')
    createMd();
    else if(current.stage == 'complete')
    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();
    }
}