Metric Instance - id - how to

Annette Kitzmil
Tera Guru

Hello,

So I have created a field on our main table with type - Reference that is referencing the metric instance table.  I am then creating a script to calculate the duration of the sys_created_on date that is being retrieved from the metric_instance table and then calculating the duration in days.  The script below does everything except recognizing the sys_id dynamically so that it is prefilling the days_pending field on my table.  Can someone take a look and let me know what I need to change to dynamically have it recognize the sys_id please?

 

(function calculatedFieldValue(current) {
 
    var mi = new GlideRecord("metric_instance");
    mi.addQuery('definition', 'f5f58c44c39802503646468dc00131f9'); //Query CDEP metric definition sys_id
    mi.addEncodedQuery('id', this.current.sys_id);
    mi.orderByDesc('sys_created_on');
    mi.query();
    if (mi.next()) {

        var todaysDate = new GlideDate(); //format 2024-07-02
        var startDate = new GlideDate();
        startDate.setValue(mi.getValue('sys_created_on')); //format 2024-05-20 13:09:20

        var dur = GlideDate.subtract(startDate, todaysDate);
        gs.info('Duration: ' + dur.getDayPart());
        days_pending = dur.getDayPart();
        return(days_pending);
    }   
 
})(current);
 
Thanks,
1 ACCEPTED SOLUTION

Annette Kitzmil
Tera Guru

Never mind, I figured it out without using the metric_instance id, below is my final code that I ended up using:

var days_pending = 0;
        var todaysDate = new GlideDate(); //format 2024-07-02
        var startDate = new GlideDate();
        startDate.setValue(current.sys_updated_on); //format 2024-05-20 13:09:20

        var dur = GlideDate.subtract(startDate, todaysDate);
       
        days_pending = dur.getDayPart();
        return(days_pending.toString());

View solution in original post

1 REPLY 1

Annette Kitzmil
Tera Guru

Never mind, I figured it out without using the metric_instance id, below is my final code that I ended up using:

var days_pending = 0;
        var todaysDate = new GlideDate(); //format 2024-07-02
        var startDate = new GlideDate();
        startDate.setValue(current.sys_updated_on); //format 2024-05-20 13:09:20

        var dur = GlideDate.subtract(startDate, todaysDate);
       
        days_pending = dur.getDayPart();
        return(days_pending.toString());