Create Metric Definition for only catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 12:36 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 01:39 AM
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 01:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 04:25 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2025 05:54 AM - edited 05-20-2025 05:55 AM
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