Metric definition script issue

zheil
Mega Expert

Hi

 

I have an issue with my metric definition script.

 

The metric does not work and I'm not sure if I used the not null condition check correctly, or something else is wrong. Even if a try null, 'NULL', or "NULL" it does not work.

 

I would like the metric to fire when the u_root_cause_ci field is populated for the first time.

 

Can anyone help?

 

if(current.u_root_cause_ci != 'NULL'){

createMatricx(current);  

}  

function createMatricx(current) {

  var mi = new MetricInstance(definition, current);

  if (mi.metricExists())

      return;

 

  var gr = mi.getNewRecord();

  gr.start = current.sys_created_on;

  gr.end = current.sys_updated_on;

  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());

//   gr.duration = gs.dateDiff(gr.start, gr.end);

  gr.calculation_complete = true;

  gr.insert();

}

1 ACCEPTED SOLUTION

marcguy
ServiceNow Employee
ServiceNow Employee

yes, sounds like it try printing what it sees as a value right at the start..



gs.log(current.u_root_cause_ci);


View solution in original post

6 REPLIES 6

marcguy
ServiceNow Employee
ServiceNow Employee

You can just use if(!current.u_root_cause.nil())


Thanks for your comment mguy, unfortunately the proposed solution is not working. Any idea what can be wrong?


marcguy
ServiceNow Employee
ServiceNow Employee

I would add a couple of gs.log statements into your script to see how far your getting, because some condition must not be getting satisfied. ie.



if(!current.u_root_cause_ci.nil()){


gs.log('current roots cause is empty - attempting to create Matricx','matrix.definition');



createMatricx(current);


}


function createMatricx(current) {


  var mi = new MetricInstance(definition, current);


  if (mi.metricExists())


gs.log('metric exists already so stopping','matrix.definition');


      return;



gs.log('attempting to insert record','matrix.definition');


  var gr = mi.getNewRecord();


  gr.start = current.sys_created_on;


  gr.end = current.sys_updated_on;


  gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());


//   gr.duration = gs.dateDiff(gr.start, gr.end);


  gr.calculation_complete = true;


  gr.insert();


}


I have updated the script and updated a relevant record, but couldn't find any of the above log entries in the Script Logs. So it means that the if condition check fails, right?