Getting metric instance record of particular incident

Sri29
Tera Contributor

Trying to retrieve the metric instance record (XML record for same also attached below in code) and not sure what am i missing.

 

I tried various addQuery options, but nothing seems to work with below background script and not quite sure how to debug if addQuery parameters are actually taking effect. I can retrieve all values if I pass metric instance id though. Please advise.?

 

 

var miInst = new GlideRecordSecure('metric_instance');

//miInst.get('1ff3a57593484210100a76de3bba10be');
var meDef = new GlideRecord('metric_definition');
meDef.get('b12d5acc93000210100a76de3bba10c9');
miInst.addQuery("definition", meDef.getUniqueValue());
gs.print('meDef.getUniqueValue(): ' + meDef.getUniqueValue());

//instance.addQuery("table", 'this.table');
miInst.addQuery('table', 'incident');
//miInst.addQuery('id', '05df73db9333f510100a76de3bba1078');

miInst.setLimit(1);
miInst.query();
var fields = miInst.getFields();
for (var i = 0; i < fields.size(); i++) {
    var field = fields.get(i);
    var name = field.getName();
    var value = field.getDisplayValue();
    gs.print(i + ". " + name + "=" + value);
}
==

// <unload unload_date="2024-02-04 05:31:26">
// <metric_instance action="INSERT_OR_UPDATE">
// <business_duration/>
// <calculation_complete>false</calculation_complete>
// <definition display_value="Time Worked">b12d5acc93000210100a76de3bba10c9</definition>
// <duration/>
// <end/>
// <field>time_worked</field>
// <field_value>1970-01-01 00:17:41</field_value>
// <id>05df73db9333f510100a76de3bba1078</id>
// <start>2024-01-30 22:13:55</start>
// <sys_created_by>system</sys_created_by>
// <sys_created_on>2024-01-30 22:13:55</sys_created_on>
// <sys_id>1ff3a57593484210100a76de3bba10be</sys_id>
// <sys_mod_count>0</sys_mod_count>
// <sys_updated_by>system</sys_updated_by>
// <sys_updated_on>2024-01-30 22:13:55</sys_updated_on>
// <table>incident</table>
// <value>17 Minutes</value>
// </metric_instance>
// </unload>

 

3 REPLIES 3

BalaG
Kilo Sage

Hello @Sri29   there are no records that satisfy your filter conditions, hence the empty values.

Add the following before your query statement to see that there are no records. There are none on my PDI.

miInst.query();
if ( ! miInst.next()) gs.print( 'no records found');

 

 

If this answer solved your issue please mark it as a solution or mark it 👍 helpful if it was of help.

--

Bala Guthy

 

 

 

Sri29
Tera Contributor

Thanks @BalaG for your response. I could find the next() call missing in my script. there is a record in my instance and it worked once I added next() call.

Sri29
Tera Contributor

Found the issue, seems like missed the next() call on the GlideRecord object.