Show Specific Timestamp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2026 05:29 AM
Hi Guys,
In the Metric Definition table of HRSD, I want to calculate and display time data from the HR Case (sn_hr_core_case) table with specific conditions on state transitions.
The requirement is to capture only the transition timestamps between selected states, rather than calculating full duration across all intermediate states.
Expected Behavior:
Ready → Awaiting Acceptance
- Capture:
- End time of Ready state
- Start time of Awaiting Acceptance state
- Ignore:
- Any duration or intermediate state changes between these two states
- Capture:
Awaiting Acceptance → Work in Progress
- Capture only:
- End time of Awaiting Acceptance
- Start time of Work in Progress
- Capture only:
Awaiting Acceptance → Closed Complete
- Capture only:
- End time of Awaiting Acceptance
- Start time of Closed Complete
- Capture only:
Could any one please help me to figure out the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Hi @AshutoshaP
Since you want to capture the duration only between specific current and previous values, you cannot do this with metric definitions. They don't have access to the "previous" (i.e., what changing from) with out of box functionality. To accomplish you can create the "Script calculation" metric definition type with no code, then use a business rule (that has access to previous and current values) to insert the actual metric instance based on your needs. See below for example of going from Ready to Work In Progress duration calculation and creating metric instance.
Metric Definition:
Business Rule
Advanced Script in BR - be sure to update to your desired state value changes and sys_id to your metric defintion.
(function executeRule(current, previous /*null when async*/ ) {
/* Wanting to track the duration of time between very specific state before/after values.
Since metric definitions do not have access to Previous and Current values natively, we are using this BR to do so
*/
if (previous.state == '10' && current.state == '18') { //Going from Ready to Work in Progress
var metricSysID = 'INSERT SYS_ID OF YOUR METRIC DEFINITION HERE'; //metric definition sys_id
var mi = new GlideRecord('metric_instance');
mi.addQuery('id', current.sys_id);
mi.addQuery('definition', metricSysID);
mi.query();
if (!mi.next()) {
insertMetrics();
}
// since !mi.next inserts new, this will insert updates
else if (current.active == true && current.operation() == 'update') {
insertMetrics();
}
}
function insertMetrics() {
var mi = new GlideRecord('metric_instance');
var metricSysID = 'INSERT SYS_ID OF YOUR METRIC DEFINITION HERE'; //metric definition sys_id
mi.initialize();
mi.definition = metricSysID;
var miStart = new GlideDateTime(previous.sys_updated_on);
var miEnd = new GlideDateTime(current.sys_updated_on);
var dur = new GlideDuration();
dur = GlideDateTime.subtract(miStart, miEnd);
mi.start = miStart;
mi.end = miEnd;
mi.duration = dur;
mi.id = current.sys_id;
mi.calculation_complete = true;
mi.insert();
}
gs.info('HR Case Metric trigger - Ready to WIP');
})(current, previous);