Metric Definition not firing on alm_hardware

ron_tavares
ServiceNow Employee
ServiceNow Employee

I am trying to create a very simple Metric Definition on the alm_hardware table.   All I need to capture is a change to the assigned_to.   I've tried this as both a Field value Duration and as a Script Calculation and still nothing happens.   I am certain the Metric is not firing at all because I tried putting some gs.log in the script, with nothing else and nothing comes up.   The very same Metric that I have on Incident works fine.   But I tried it on both alm_asset and alm_hardware, and nothing happens.     I am getting the same results on both the customer's instance and my personal instance.   I even tried changing the field to something else, like state [install_state], still nothing happens.   It seems like Metrics don't like to run on asset tables?   Is there something I am missing from a Metric Definition configuration perspective?

Thanks in advance,

.ron

1 ACCEPTED SOLUTION

Dennis R
Tera Guru

Sorry, my answer above was correct, but didn't really fully answer how to do what you're trying to do.   You'll need to duplicate the metric events business rule, running it on the table you're interested in.   In your specific case, I'd suggest running it on the Asset [alm_asset] table, since the asset_hardware table extends it.   That way you can collect metrics on other asset types later if you want.



To do so, create a new business rule and name it something like metric events (asset), and run it on the Asset [alm_asset] table.   Check the Advanced box, and make the following selections:


  • When: before
  • Order: 10000
  • Insert: (selected)
  • Update: (selected)
  • Delete: (selected)
  • Query: (unselected)
  • Actions: (leave everything to its default, with no field values selected, no messages added, and the abort action field unselected)
  • Advanced / Condition: (leave blank)
  • Script (copy from below)


As for the script, I personally prefer the non-global-namespace polluting closure syntax, so this is what I use.   I tested it in my personal instance and it works like a charm:



(function executeRule(current, previous) {


      var getDefinitions = function(fields) {


              var gr = new GlideAggregate('metric_definition');


              gr.addActiveQuery();


              var tables = GlideDBObjectManager.getTables(current.getTableName());


              gr.addQuery('table', tables);


              gr.addQuery('field', fields);


              gr.groupBy('field');


              gr.query();


              return gr;


      };




      var gru =   new GlideScriptRecordUtil.get(current);


      var fieldsChanged = gru.getChangedFieldNames();


      var gr = getDefinitions(fieldsChanged);


      fields = '';


      while (gr.next())


              fields += gr.field + ',';


   


      if (fields.length > 0) {


              fields = '[' + fields.substring(0, fields.length - 1) + ']';


              gs.eventQueue('metric.update', current, fields, current.sys_mod_count, 'metric_update');


      }


})(current, previous);



Save it, and now your metric definitions on any Asset table will work correctly.   Here's a sample one I created that's working great in my personal instance:


metric definition.png



If this solves your issue, please mark my answer correct.   If not, please let me know what issue you're experiencing and I'll try to help if I can.



Regards,


--Dennis R


View solution in original post

3 REPLIES 3

Dennis R
Tera Guru

Out of the box, metrics only work on the task table and tables derived from the task table.   To create a metric on a non-task table, you'll need to create a business rule that runs on your table.   See the bottom of this page for info.


Note: In the base system, metrics are configured to work on the task table only. To apply metrics to cmdb_ci tables, duplicate the metric events business rule that currently runs on the task table for the cmdb_ci table. Without the events created, no metric processing can occur.


Hope this helps,


--Dennis R


Dennis R
Tera Guru

Sorry, my answer above was correct, but didn't really fully answer how to do what you're trying to do.   You'll need to duplicate the metric events business rule, running it on the table you're interested in.   In your specific case, I'd suggest running it on the Asset [alm_asset] table, since the asset_hardware table extends it.   That way you can collect metrics on other asset types later if you want.



To do so, create a new business rule and name it something like metric events (asset), and run it on the Asset [alm_asset] table.   Check the Advanced box, and make the following selections:


  • When: before
  • Order: 10000
  • Insert: (selected)
  • Update: (selected)
  • Delete: (selected)
  • Query: (unselected)
  • Actions: (leave everything to its default, with no field values selected, no messages added, and the abort action field unselected)
  • Advanced / Condition: (leave blank)
  • Script (copy from below)


As for the script, I personally prefer the non-global-namespace polluting closure syntax, so this is what I use.   I tested it in my personal instance and it works like a charm:



(function executeRule(current, previous) {


      var getDefinitions = function(fields) {


              var gr = new GlideAggregate('metric_definition');


              gr.addActiveQuery();


              var tables = GlideDBObjectManager.getTables(current.getTableName());


              gr.addQuery('table', tables);


              gr.addQuery('field', fields);


              gr.groupBy('field');


              gr.query();


              return gr;


      };




      var gru =   new GlideScriptRecordUtil.get(current);


      var fieldsChanged = gru.getChangedFieldNames();


      var gr = getDefinitions(fieldsChanged);


      fields = '';


      while (gr.next())


              fields += gr.field + ',';


   


      if (fields.length > 0) {


              fields = '[' + fields.substring(0, fields.length - 1) + ']';


              gs.eventQueue('metric.update', current, fields, current.sys_mod_count, 'metric_update');


      }


})(current, previous);



Save it, and now your metric definitions on any Asset table will work correctly.   Here's a sample one I created that's working great in my personal instance:


metric definition.png



If this solves your issue, please mark my answer correct.   If not, please let me know what issue you're experiencing and I'll try to help if I can.



Regards,


--Dennis R


ron_tavares
ServiceNow Employee
ServiceNow Employee

That worked awesomely!  



Thanks Dennis!