Create Metric Definition for only catalog item

Ashwin Perumal1
Tera Contributor

Hello All,

 

I am trying to create metric definition for my catalog item RITMS to calculate the time in each stage. Currently I created a metric definition on sc_req_item table , Field is 'Stage' and Type is 'Field Value Duration'. Below is my code used in script part.

 

if (current.cat_item == 'my_cat_sys_id_here') {

    if (current.stage.changes() || current.stage == "complete" || current.stage == "Request Cancelled" || current.stage == "closed_incomplete" || current.stage == "closed_skipped") {
        answer = false;
        mi.endDuration();
        closeDurations(mi.current);
    }
}

function closeDurations(current) {
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.type', 'field_value_duration');
    gr.query();
    while (gr.next()) {
        var definition = new GlideRecord('metric_definition');
        definition.get(gr.definition);
        var mi = new MetricInstance(definition, current);
        mi.endDuration();
    }
}
 
The above code is working fine but the problem is its calculating metric for all catalog RITMS. But I need to calculate for only one specific catalog item. I have even given catalog name instead of sys_id and even changed the field type to script duration but still doesn't work. When i change to script duration my metric itself did not even create for any RITM. So only way was to have it field value duration. 
 
Can anyone pls help me to achieve this by making it calculate metric only for one catalog item as needed.
Thanks in advance!!!.


 

4 REPLIES 4

pratikjagtap
Giga Guru

Hi @Ashwin Perumal1 ,

Step 1: Keep your Metric Definition as Field Value Duration

  • Table: sc_req_item
  • Field: stage
  • Type: Field Value Duration
  • No need to put complex filtering logic in the metric script itself (just basic stage check is enough)

Step 2: Use a Business Rule to avoid triggering metrics for unrelated RITMs
Because Field Value Duration metric definitions are started automatically on field change, the only way to prevent metrics for other catalog items is to prevent the metric from being created at all by deleting or closing it immediately when it’s not your item.

 

(function executeRule(current, previous /*null when async*/) {
var MY_CAT_ITEM = 'my_cat_sys_id_here';

if (current.cat_item != MY_CAT_ITEM) {
// Close all running metric instances for non-target catalog items
var gr = new GlideRecord('metric_instance');
gr.addQuery('id', current.sys_id);
gr.addQuery('calculation_complete', false);
gr.addQuery('definition.type', 'field_value_duration');
gr.query();
while (gr.next()) {
gr.calculation_complete = true;
gr.update();
}
}
})();

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik

Mark Manders
Mega Patron

At this moment you only need it for that one item. And in 3 months someone is asking the same for another item. Why not just have it calculate stage duration for all and you only report on the one that you need? It's asking a little bit more from the system, but not that much and in the future, you will be thanked for having the data present for reporting on others.
And you could even get rid of your script and just have the duration run on 'stage' change and then report on the new value of any of your closed states.


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

Hi Mark,

As per your suggestion then I don't need to use the script field itself. My field value when set to stage by default it is calculating for all stages and for RITMS. But basically I am little curious on understanding whats doing wrong in my script.

You could try it the other way around (if not your sysid, then do xyz): 

function closeDurations(current) {
    if (current.cat_item != 'my_cat_sys_id_here') return;

    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('calculation_complete', false);
    gr.addQuery('definition.type', 'field_value_duration');
    gr.query();
    while (gr.next()) {
        var definition = new GlideRecord('metric_definition');
        definition.get(gr.definition);
        var mi = new MetricInstance(definition, current);
        mi.endDuration();
    }
}

(and yes: no need to script at all, just add conditions to the report, because you can trust me on this: this will become something you will have to do for other items as well).


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