Metric Definition - How to add OR condition in this script?

Mark Nguyen
Tera Guru

Hello ServiceNow,

 

I have below Metric Defintion script that creates a Metric Instance for incidents opened where user's title is IS SERVICE SPT SRVC DESK TSR 1. This is working as intended however I need to include other titles e.g IS SERVICE SPT SRVC DESK TSR 2, IS SERVICE SPT SRVC DESK TSR 4.

 

How can I modify the code below to include the other titles?

if (current.opened_by.title == "IS SERVICE SPT SRVC DESK TSR 1") {
  var value = true;    
  createMetric(value);
}
function createMetric(value) {
  var mi = new MetricInstance(definition, current);
  if (mi.metricExists()) 
    return;

  var gr = mi.getNewRecord();
  gr.field_value = value;
  gr.field = null;
  gr.calculation_complete = true;
  gr.insert();
}

 Title field is a string field.

 

Thank you

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Hi,

 

change the first line as follows:

 

if ((current.opened_by.title == "IS SERVICE SPT SRVC DESK TSR 1") ||
   (current.opened_by.title == "second value") ||
    (current.opened_by.title == "third value") ) {

simple javascript 'or' operator.

View solution in original post

3 REPLIES 3

Bert_c1
Kilo Patron

Hi,

 

change the first line as follows:

 

if ((current.opened_by.title == "IS SERVICE SPT SRVC DESK TSR 1") ||
   (current.opened_by.title == "second value") ||
    (current.opened_by.title == "third value") ) {

simple javascript 'or' operator.

Thank you @Bert_c1 this worked. I really appreciate your response and time.

Yamsanibhavani
Giga Guru

Hi @Mark Nguyen 

Could you please send the script include code (metricInstance) which you have used, working with same metric definitions but not showing the metric records. I want to re-verify where I am missing.

 

Thanks in advance