First Call resolution Metric for HRSD
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2025 04:33 AM
Hi everyone,
I'm trying to create a metris that shows true everytime an hr case state changes from awaiting acceptance to Work in Progress.
However, my script calculation doesn't seem to work. Need some help in identifying what I'm doing wrong
I've attached my script below
if (current.state.changes() && current.state == "18")
{
var previous = current.state.getPreviousValue();
if (previous == "20") {
createMetric(true);
}
}
function createMetric(value){
var mi = new MetricInstance(definition, current);
if (mi.metricExists()) {
return;
}
var gr = mi.getNewRecord();
gr.field_value = value;
gr.field = null;
gr.calculation_complete = true;
gr.insert();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 05:11 AM
Metric's don't have access to the previous value, only the changed value. I would change your logic to create the metric instance when the state is awaiting acceptance, and close it when in WIP.
//answer and mi are passed in as part of MetricInstance script include
//calling eval()
(function(definition, current , answer, mi) {
//using type 'Field value duration' to cater for
//scenarios where state could cycle, and we want to create
//a new metric instance each time
//Cancel existing metrics and create a new one
if(current.getValue(definitionField) == sn_hr_core.hr.STATE_AWAITING_ACCEPTANCE){
answer = true; //will end any previous metric instances, and create a new one
return;
}
if(current.getValue(definitionField) == sn_hr_core.hr.STATE_WORK_IN_PROGRESS){
mi.endDuration();
answer = false; //will end any previous metric instances, and not create a new one
return;
}
})(definition, current, answer, mi);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2025 01:08 AM
Thanks Kieran,
I did change my script to use the audit table. It works for the purpose I intended but I'm afraid it may cause performance issues. This is my new script.
// Variables available:
// current: GlideRecord (target HR case)
// definition: GlideRecord (this metric definition)
var previousState = getPreviousState(current.sys_id, "state");
var assignmentGroup = current.assignment_group ? current.assignment_group.toString() : "";
var allowedGroups = [
"e0cd53df1b4a021033fc855bd34bcbad",
"d02ed79f1b4a021033fc855bd34bcb5a",
];
if (previousState == "20" && current.state == "18" && allowedGroups.includes(assignmentGroup)) { // 20 = Awaiting Acceptance, 18 = Work in Progress
createMetric(true);
}
// Function to get the previous state from sys_audit
function getPreviousState(recordSysId, fieldName) {
var auditGR = new GlideRecord("sys_audit");
auditGR.addQuery("documentkey", recordSysId);
auditGR.addQuery("fieldname", fieldName);
auditGR.orderByDesc("sys_created_on");
auditGR.setLimit(1);
auditGR.query();
if (auditGR.next()) {
return auditGR.oldvalue; // Return the last known state
}
return null; // Return null if no history found
}
// Function to create a metric instance
function createMetric(value) {
var mi = new MetricInstance(definition, current);
if (mi.metricExists()) {
return;
}
var gr = mi.getNewRecord();
gr.field_value = value;
gr.field = null;
gr.calculation_complete = true;
gr.insert();
}
I'll test your script and let you know what I find. Thanks a lot

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2025 03:04 AM
Hi,
Querying the sys_audit table is indeed strongly advised against. Did my script work? Did you try it?