Metric definition defined but not creating metric instances for sys_updated_by field

Ravi Gupta1
Tera Expert

Metric definition defined but not creating metric instances for sys_updated_by field.

Any suggestion, how to achieve it.

1 ACCEPTED SOLUTION

@Ravi Gupta1 

ServiceNow doesn't monitor OOTB system fields (sys_created_by, sys_updated_by) so metric instances won't trigger unless you add custom logic

use this script in the Metric definition

createMetric(current.sys_updated_by.toString());

function createMetric(value) {
    var mi = new MetricInstance(definition, current);
    var gr = mi.getNewRecord();
    gr.field_value = value;
    gr.calculation_complete = true;
    gr.insert();
}

then have after update business rule on incident table to trigger metric.update event

this will fire the event every time the record is updated allowing your metric definition to create desired metric instances

(function executeRule(current, previous /*null when async*/) {
    gs.eventQueue('metric.update', current, '[sys_updated_by]', 1, 'metric_update');
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Ravi Gupta1 

share what config you did for that

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Below is the screenshot of metric definition. I am able to create metric instance for other fields but not for sys field for same table.

metric definition.png

@Ravi Gupta1 

ServiceNow doesn't monitor OOTB system fields (sys_created_by, sys_updated_by) so metric instances won't trigger unless you add custom logic

use this script in the Metric definition

createMetric(current.sys_updated_by.toString());

function createMetric(value) {
    var mi = new MetricInstance(definition, current);
    var gr = mi.getNewRecord();
    gr.field_value = value;
    gr.calculation_complete = true;
    gr.insert();
}

then have after update business rule on incident table to trigger metric.update event

this will fire the event every time the record is updated allowing your metric definition to create desired metric instances

(function executeRule(current, previous /*null when async*/) {
    gs.eventQueue('metric.update', current, '[sys_updated_by]', 1, 'metric_update');
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

I updated the code as mentioned by you. But in metric instance table value field shows my user_name even if other use update the ticket.

I also added logs in Metric Definition to check the value of "current.sys_updated_by". It always shows my user_name if I updated the ticket first and later updated by other user.

 

Suppose userB updated the ticket first and later userC updated the ticket. Metric instance and system logs shows only userB every time.

 

gs.log('user_id: ' +current.sys_updated_by.toString());

createMetric(current.sys_updated_by.toString());

function createMetric(value) {
var mi = new MetricInstance(definition, current);
var gr = mi.getNewRecord();
gr.field_value = value;
gr.calculation_complete = true;
gr.insert();
}

 

Can you please assist.

Thanks

Ravi