<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Creating Metric Instances through Scheduled jobs in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526795#M183721</link>
    <description>&lt;P&gt;Thanks so much, Kalai!&lt;/P&gt;&lt;BR /&gt;</description>
    <pubDate>Tue, 15 Nov 2016 22:36:50 GMT</pubDate>
    <dc:creator>jleyco</dc:creator>
    <dc:date>2016-11-15T22:36:50Z</dc:date>
    <item>
      <title>Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526788#M183714</link>
      <description>&lt;P&gt;Hello everyone.&lt;/P&gt;&lt;P&gt;So, for reporting reasons, I need each first day of the month to create a new metric instance from a specific definition.&lt;/P&gt;&lt;P&gt;So of course I thought of creating a scheduled job, but for some reason it goes through everything but my instances are not created...&lt;/P&gt;&lt;P&gt;Would anyone have an idea as to where I'm taking a wrong turn? &lt;/P&gt;&lt;P&gt;Thank you so much.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;here's my code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;createMetricInstances();&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;function createMetricInstances(){&lt;/P&gt;&lt;P&gt; &amp;nbsp; var value = 'FollowUp Indicator not breached';&lt;/P&gt;&lt;P&gt;//Query on the task sla table to know exactly how many instances I need to create&lt;/P&gt;&lt;P&gt; &amp;nbsp; var gr = new GlideRecord("task_sla");&lt;/P&gt;&lt;P&gt; &amp;nbsp; gr.addQuery("sla", "37b3b0ea3774e600894b98a543990e29");&lt;/P&gt;&lt;P&gt; &amp;nbsp; gr.addActiveQuery();&lt;/P&gt;&lt;P&gt; &amp;nbsp; gr.query();&lt;/P&gt;&lt;P&gt; &amp;nbsp; while (gr.next()) { &amp;nbsp; &lt;/P&gt;&lt;P&gt;// Here I tried several ways to pass values to definition and current. Nothing worked&lt;/P&gt;&lt;P&gt; &amp;nbsp; var newmi = new MetricInstance('c083fcaa3774e600894b98a543990e48', gr);&lt;/P&gt;&lt;P&gt; &amp;nbsp; /*var mi = new MetricInstance(def, gr);&lt;/P&gt;&lt;P&gt; &amp;nbsp; var newmi = mi.getNewRecord();&lt;/P&gt;&lt;P&gt; &amp;nbsp; newmi.definition = 'c083fcaa3774e600894b98a543990e48';&lt;/P&gt;&lt;P&gt; &amp;nbsp; newmi.id = gr.sys_id;*/&lt;/P&gt;&lt;P&gt; &amp;nbsp; var now = new GlideDateTime();&lt;/P&gt;&lt;P&gt; &amp;nbsp; newmi.start = now;&lt;/P&gt;&lt;P&gt; &amp;nbsp; if (gr.has_breached == 'true'){&lt;/P&gt;&lt;P&gt; &amp;nbsp; value = 'FollowUp Indicator breached';&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt; &amp;nbsp; newmi.value = value;&lt;/P&gt;&lt;P&gt; &amp;nbsp; newmi.insert();&lt;/P&gt;&lt;P&gt; &amp;nbsp; //I've set a serie of logs here and there which I erased here for easier reading - They all log correctly...&lt;/P&gt;&lt;P&gt; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2016 12:09:02 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526788#M183714</guid>
      <dc:creator>julienr</dc:creator>
      <dc:date>2016-09-07T12:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526789#M183715</link>
      <description>&lt;P&gt;Anyway pasting this here as well.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;A title="Re: Need help to create metric using the Updated by field." __default_attr="998988" __jive_macro_name="message" class="jive_macro jive_macro_message" data-orig-content="Re: Need help to create metric using the Updated by field." data-renderedposition="31_8_386_16" href="https://www.servicenow.com/community?id=community_question&amp;amp;sys_id=bc25876ddbd8dbc01dcaf3231f961959"&gt;Re: Need help to create metric using the Updated by field.&lt;/A&gt; &lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 07 Sep 2016 12:34:33 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526789#M183715</guid>
      <dc:creator>Kalaiarasan Pus</dc:creator>
      <dc:date>2016-09-07T12:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526790#M183716</link>
      <description>&lt;P&gt;So, I managed to solve this using a related conversation I found: The script is aimed to be a scripted scheduled job and is functional&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;createMetricInstances();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;function createMetricInstances(){&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var value;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var def = 'c083fcaa3774e600894b98a543990e48';//Replace value with the MetricDef you want to instanciate&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var gr = new GlideRecord("task_sla");&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gr.addQuery("sla", "37b3b0ea3774e600894b98a543990e29");//Replace value with the sla definition sys_id&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gr.addActiveQuery();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gr.query();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; //gs.log('JU:Scheduled job found '+gr.getRowCount()+' rows');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; while (gr.next()) {&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; value = 'FollowUp Indicator not breached';&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var newmi = new GlideRecord('metric_instance');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.initialize();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.definition = def;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.id = gr.sys_id;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.start = gs.nowDateTime();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; if (gr.has_breached == true)&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; value = 'FollowUp Indicator breached';&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.value = value;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.insert();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; }&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;}&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 07 Sep 2016 13:02:33 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526790#M183716</guid>
      <dc:creator>julienr</dc:creator>
      <dc:date>2016-09-07T13:02:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526791#M183717</link>
      <description>&lt;P&gt;This was very helpful! &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;However, I was wondering how you would modify this to NOT create a metric instance if there is already one for that 'ID' and metric definition?&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Nov 2016 20:25:54 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526791#M183717</guid>
      <dc:creator>jleyco</dc:creator>
      <dc:date>2016-11-15T20:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526792#M183718</link>
      <description>&lt;P&gt;You can find that in the original link provided. I would have checked if the record already exists before inserting a new record.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Nov 2016 20:27:56 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526792#M183718</guid>
      <dc:creator>Kalaiarasan Pus</dc:creator>
      <dc:date>2016-11-15T20:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526793#M183719</link>
      <description>&lt;P&gt;Thanks, Kalaiarasan! But I'm not sure how to apply that logic to my Scheduled Script Execution below. Could you advise?&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;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.&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;PRE __default_attr="java" __jive_macro_name="code" class="_jivemacro_uid_147924188788816 jive_macro_code jive_text_macro" data-renderedposition="134_8_1192_384" jivemacro_uid="_147924188788816" modifiedtitle="true"&gt;&lt;P&gt;createMetricInstances();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;function createMetricInstances(){&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var value;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var def = 'a1e2781f131d22000a2033076144b0c5';//MTRC0010006 Create to Close Duration of Catalog Tasks&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var gr = new GlideRecord('sc_task');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gr.addQuery('closed_at', '&amp;lt;=', '2016-10-31 00:00:00');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gr.setLimit(10);&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gr.query();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; gs.log('Created to Close Duration of Closed Catalog Tasks scheduled job found '+gr.getRowCount()+' rows');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; while (gr.next()) {&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; var newmi = new GlideRecord('metric_instance');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.initialize();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.definition = def;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.id = gr.sys_id;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.start = gr.sys_created_on;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.end = gr.closed_at;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.duration = gs.dateDiff(gr.sys_created_on.getDisplayValue(), gr.closed_at.getDisplayValue());&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.calculation_complete = true;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.field = 'State';&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.insert();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; }&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;}&lt;/P&gt;&lt;BR /&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Nov 2016 20:33:05 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526793#M183719</guid>
      <dc:creator>jleyco</dc:creator>
      <dc:date>2016-11-15T20:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526794#M183720</link>
      <description>&lt;P&gt;function createMetricInstances(){ &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;var value; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;var def = 'a1e2781f131d22000a2033076144b0c5';//MTRC0010006 Create to Close Duration of Catalog Tasks &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;var gr = new GlideRecord('sc_task'); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;gr.addQuery('closed_at', '&amp;lt;=', '2016-10-31 00:00:00'); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;gr.setLimit(10); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;gr.query(); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;gs.log('Created to Close Duration of Closed Catalog Tasks scheduled job found '+gr.getRowCount()+' rows'); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;while (gr.next()) { &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;var newmi = new GlideRecord('metric_instance');&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;newmi.addQuery('definition',def);&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;newmi.addQuery('id',gr.sys_id);&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;newmi.query();&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;if(!newmi.next())&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;{&lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.initialize(); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.definition = def; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.id = gr.sys_id; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.start = gr.sys_created_on; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.end = gr.closed_at; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.duration = gs.dateDiff(gr.sys_created_on.getDisplayValue(), gr.closed_at.getDisplayValue()); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.calculation_complete = true; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.field = 'State'; &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt; &amp;nbsp; newmi.insert(); &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;} &amp;nbsp; &lt;/P&gt;&lt;BR /&gt;&lt;P&gt;}&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;} &lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Nov 2016 20:37:14 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526794#M183720</guid>
      <dc:creator>Kalaiarasan Pus</dc:creator>
      <dc:date>2016-11-15T20:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526795#M183721</link>
      <description>&lt;P&gt;Thanks so much, Kalai!&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Nov 2016 22:36:50 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526795#M183721</guid>
      <dc:creator>jleyco</dc:creator>
      <dc:date>2016-11-15T22:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Metric Instances through Scheduled jobs</title>
      <link>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526796#M183722</link>
      <description>&lt;P&gt;Thank you guys! worked perfectly for me.&lt;/P&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 13 Dec 2017 20:37:48 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/creating-metric-instances-through-scheduled-jobs/m-p/1526796#M183722</guid>
      <dc:creator>Community Alums</dc:creator>
      <dc:date>2017-12-13T20:37:48Z</dc:date>
    </item>
  </channel>
</rss>

