Need to create Metric instance record based on certain condition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 08:49 AM
Hi,
I want to create a metric instance record but on specific condition.
I have created one metric definition : Last assignment group for Incident table where type is script calculation. But I haven't written script on the script field. Rather I have written before business rule on incident table for insert and update operation.
In the when to run, I have given condition as Assignment group is "X".
The requirement is that metric should only be created when there is no existing metric record already for today's date for the same definition and ID.
The below script I have written for the same but it is not creating the metric instance record.
Script:
(function executeRule(current, previous /*null when async*/ ) {
var count=0;
var chk=new GlideRecord('metric_instance');
chk.addQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
chk.addQuery('definition','0fd336fb87730110e75c1f073cbb35e4'); // definition sys_id
chk.addQuery('id','current.sys_id'); // current incident sys_id
chk.query();
if(!chk.next());
{
count++;
gs.log("metric should create"+ count);
}
if(count>0){
createMetric();
gs.log("coming inside if condition");
}
function createMetric() {
var mi = new GlideRecord('metric_instance');
var metricSysID = '0fd336fb87730110e75c1f073cbb35e4';
mi.initialize();
mi.definition = metricSysID;
mi.start = previous.sys_updated_on;
mi.end = gs.nowDateTime();
mi.duration = gs.dateDiff(mi.start, mi.end);
mi.id = current.sys_id;
mi.field=gtfd;
mi.value = current.getDisplayValue('assignment_group');
mi.calculation_complete = true;
mi.insert();
}
})(current, previous);
Can anyone help me to find where I am wrong?