How do I access the ID from Metric instance table?

Annika Aaltonen
Kilo Guru

Hi all,

How do I access the ID data from the metric_instance table, it being a document type field?

I want to run a freeform /BG script on the metric_instance table, but for my query I need to access the value of 'active' and 'closed_at' on the ID record. Apparently you can't just access it like: 'id.active' like a reference field.

Any help is appreciated!

TIA,

Annika

var inst = new GlideRecord("metric_instance");


inst.addQuery('definition','6739a66b1b316850629d4002cd4bcb0d');

inst.addQuery('calculation_complete',false);

inst.addQuery('id.active',false);

inst.addQuery('duration', '');


inst.query();



while(inst.next()){


   inst.duration = gs.dateDiff(inst.start.getDisplayValue(), inst.id.closed_at.getDisplayValue());

   inst.calculation_complete = true;

   inst.end = inst.id.closed_at.getDisplayValue();


}
1 ACCEPTED SOLUTION

Hi Filipe,

 

Thanks! For anyone who might find this helpful, this is the finalized script that ended up working for us:

 

var inst = new GlideRecord("metric_instance");
var qc = inst.addQuery('definition','6739a66b1b316850629d4002cd4bcb0d');
qc.addOrCondition('definition', '6b1926ab1b316850629d4002cd4bcb32');
inst.addQuery('calculation_complete',false);
inst.addQuery('duration', '');
inst.query();

while(inst.next()){
var tableName = inst.table.toString();
var ticket = new GlideRecord(tableName);
ticket.addQuery('sys_id', inst.id.toString());
ticket.addQuery('active', false);
ticket.query();

if (ticket.next()){
inst.duration = gs.dateDiff(inst.start.getDisplayValue(), ticket.closed_at.getDisplayValue());
inst.calculation_complete = true;
inst.end = ticket.closed_at.getDisplayValue();
inst.update();
}
}

 

Best,

Annika

View solution in original post

7 REPLIES 7

Filipe Cruz
Kilo Sage
Kilo Sage
Hello Annika, Open the XML record of the metric instance and you will find a field called (I think!) Something like "document_table" the will contain the table name of the document Id field. With that you will be able to do your query and get the desired data. Please, if you consider my answer relevant, mark it as correct answer or helpful! Best Regards, Filipe Cruz

Hi Filipe,

 

Thanks for your reply!

This is what the XML looks like as an example:

 

<xml>
<metric_instance>
<business_duration/>
<calculation_complete>false</calculation_complete>
<definition display_value="State RITM">27892baa8759f8101480a6c73cbb350d</definition>
<duration/>
<end/>
<field>state</field>
<field_value>5</field_value>
<id>4f9efba987ea851013c24086cebb358b</id>
<start>2022-03-30 13:10:22</start>
<sys_created_by>system</sys_created_by>
<sys_created_on>2022-03-30 13:10:25</sys_created_on>
<sys_id>0cd4447d87a6c5101480a6c73cbb359d</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>system</sys_updated_by>
<sys_updated_on>2022-03-30 13:10:25</sys_updated_on>
<table>sc_req_item</table>
<value>Solution Proposed</value>
</metric_instance>
</xml>
 
 
There is only the field 'table', did you mean that one? Still unsure of how to access my ID record from just the table name.

Hello!

 Yes, the table field!!

You can perform a gliderecord with:

var gr = new GlideRecord(current.table);
gr.get(current.id);

gs.print(gr.active);

 

Please mark my answer as correct and helpful if it is relevant for you.

 

Best Regards,

 

Filipe Cruz

 

Thanks Filipe, please allow me some time to check this solution and I will get back to you!