Need to change Metric Instance "Created by / Updated by" to the user who made the change
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 12:06 PM - edited 09-11-2024 01:26 PM
Hello,
We noticed that metric instances are being defualted to "system" on created by and updated by, reading SN documentation this appears to be normal and per dis article this can be changed ( Click here to see KB article ), however we weren't unable to make this work, the reason of this is because we are doing a report via Database view (metric instance table, metric def table and custom table) and we need to know what user made the change on the specific field. The code did not create any new Metric instance, however when the "gr.autoSysFields(false);" line is removed then it works but did not overwrite the Updated by and Created by fields
This is the MetricInstance Script include modified per KB article
gs.include("PrototypeServer");
var MetricInstance = Class.create();
MetricInstance.prototype = {
initialize: function(definitionGR, currentGR) {
this.definition = definitionGR;
this.current = currentGR;
},
// process is the driver for field value duration type definitions
process: function() {
answer = true;
mi = this; // global variable
eval(this.definition.script);
if (!answer)
return;
this.endDuration(); // end any previous duration for this metric
this.startDuration(); // start a new one
},
startDuration: function() {
try {
var gr = this.getNewRecord();
gr.field_value = this.current[this.definition.field];
gr.start = current.sys_updated_on;
gr.sys_updated_on = current.sys_updated_on;
gr.sys_updated_by = current.sys_updated_by;
gr.sys_created_on = current.sys_created_on;
gr.sys_created_by = current.sys_created_by;
gr.sys_mod_count = 0;
gr.autoSysFields(false);
gr.insert();
} catch (ex) {
gs.error("metricError:: " + ex);
}
},
endDuration: function() {
try {
var gr = new GlideRecord('metric_instance');
gr.addQuery('definition', this.definition.sys_id);
gr.addQuery('id', this.current.sys_id);
gr.addQuery('calculation_complete', false);
gr.query();
if (!gr.next())
return;
gr.end = this.current.sys_updated_on;
gr.duration = gs.dateDiff(gr.start.getDisplayValue(), gr.end.getDisplayValue());
gr.calculation_complete = true;
gr.sys_updated_on = current.sys_updated_on;
gr.sys_updated_by = current.sys_updated_by;
if (gr.sys_created_on != current.sys_updated_on) {
gr.sys_mod_count = 1;
}
// gr.autoSysFields(false);
gr.update();
} catch (ex) {
gs.error("metricError:: " + ex);
}
},
getNewRecord: function() {
var gr = new GlideRecord('metric_instance');
gr.table = this.current.getRecordClassName();
gr.id = this.current.sys_id;
gr.definition = this.definition.sys_id;
gr.field = this.definition.field;
return gr;
},
// return true if a metric exists for this definition and current
metricExists: function() {
var gr = new GlideRecord('metric_instance');
gr.addQuery("id", this.current.sys_id);
gr.addQuery("definition", this.definition.sys_id);
gr.query();
return gr.hasNext();
},
_z: function() {},
}
You can easily find yours on your PDI by going here:
https://<INSTANCE>/nav_to.do?uri=sys_script_include.do?sys_id=44c7c3a40a25810200e0dbdf70ea7f0c
- Labels:
-
MetricBase