Creating Metric Instances through Scheduled jobs

julienr
Mega Contributor

Hello everyone.

So, for reporting reasons, I need each first day of the month to create a new metric instance from a specific definition.

So of course I thought of creating a scheduled job, but for some reason it goes through everything but my instances are not created...

Would anyone have an idea as to where I'm taking a wrong turn?

Thank you so much.

here's my code:

createMetricInstances();

function createMetricInstances(){

  var value = 'FollowUp Indicator not breached';

//Query on the task sla table to know exactly how many instances I need to create

  var gr = new GlideRecord("task_sla");

  gr.addQuery("sla", "37b3b0ea3774e600894b98a543990e29");

  gr.addActiveQuery();

  gr.query();

  while (gr.next()) {  

// Here I tried several ways to pass values to definition and current. Nothing worked

  var newmi = new MetricInstance('c083fcaa3774e600894b98a543990e48', gr);

  /*var mi = new MetricInstance(def, gr);

  var newmi = mi.getNewRecord();

  newmi.definition = 'c083fcaa3774e600894b98a543990e48';

  newmi.id = gr.sys_id;*/

  var now = new GlideDateTime();

  newmi.start = now;

  if (gr.has_breached == 'true'){

  value = 'FollowUp Indicator breached';

}

  newmi.value = value;

  newmi.insert();

  //I've set a serie of logs here and there which I erased here for easier reading - They all log correctly...

  }

}

8 REPLIES 8

Thanks, Kalaiarasan! But I'm not sure how to apply that logic to my Scheduled Script Execution below. Could you advise?



Basically I created a new metric definition to calculate the Create to Close duration of catalog tasks. This metric definition works fine for newly closed catalog tasks, but it does not apply to 'previously' closed catalog tasks. The below script runs fine, but I am currently limiting it to 10, just to see how it works. I also want to ensure that it does not create any 'duplicate' metric instances.



createMetricInstances();




function createMetricInstances(){


  var value;


  var def = 'a1e2781f131d22000a2033076144b0c5';//MTRC0010006 Create to Close Duration of Catalog Tasks


  var gr = new GlideRecord('sc_task');


  gr.addQuery('closed_at', '<=', '2016-10-31 00:00:00');


  gr.setLimit(10);


  gr.query();


  gs.log('Created to Close Duration of Closed Catalog Tasks scheduled job found '+gr.getRowCount()+' rows');


  while (gr.next()) {


  var newmi = new GlideRecord('metric_instance');


  newmi.initialize();


  newmi.definition = def;


  newmi.id = gr.sys_id;


  newmi.start = gr.sys_created_on;


  newmi.end = gr.closed_at;


  newmi.duration = gs.dateDiff(gr.sys_created_on.getDisplayValue(), gr.closed_at.getDisplayValue());


  newmi.calculation_complete = true;


  newmi.field = 'State';


  newmi.insert();


  }


}


function createMetricInstances(){  


var value;  


var def = 'a1e2781f131d22000a2033076144b0c5';//MTRC0010006 Create to Close Duration of Catalog Tasks  


var gr = new GlideRecord('sc_task');  


gr.addQuery('closed_at', '<=', '2016-10-31 00:00:00');  


gr.setLimit(10);  


gr.query();  


gs.log('Created to Close Duration of Closed Catalog Tasks scheduled job found '+gr.getRowCount()+' rows');  


while (gr.next()) {  




var newmi = new GlideRecord('metric_instance');


newmi.addQuery('definition',def);


newmi.addQuery('id',gr.sys_id);


newmi.query();


if(!newmi.next())


{


  newmi.initialize();  


  newmi.definition = def;  


  newmi.id = gr.sys_id;  


  newmi.start = gr.sys_created_on;  


  newmi.end = gr.closed_at;  


  newmi.duration = gs.dateDiff(gr.sys_created_on.getDisplayValue(), gr.closed_at.getDisplayValue());  


  newmi.calculation_complete = true;  


  newmi.field = 'State';  


  newmi.insert();  


}  


}


}


Thanks so much, Kalai!


Community Alums
Not applicable

Thank you guys! worked perfectly for me.