Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How can I add the duration of multiple metric instance records that are grouped by value ?

Ashfaq Aziz1
Tera Contributor

Hello Experts,

 

My requirement is that on the SIR record there are few fields of type glide_duration and I have created a metric definition on SIR record which runs on state change and metric records are created in the metric instance table with multiple records having duration of state (refer to the screenshots). My requirement here is that when the SIR record is resolved I need to calculate the total duration present in the metric_instance table for each state value individually and store it in the fields present in the SIR record. 

To achieve this I tried an on before BR which calculates the total duration but somehow it is setting the wrong duration in the fields which are present in the SIR record.

 

Code used in BR:

 

(function executeRule(current, previous /*null when async*/ ) {

    var metric_gr = new GlideRecord('metric_instance');
    metric_gr.addEncodedQuery('definition=5c630110ffb00200158bffffffffffed^calculation_complete=true^id=' + current.sys_id);
    metric_gr.query();

    var state_totals = {};
    while (metric_gr.next()) {

        var state_value = metric_gr.value.toString();
        gs.info('state = ' + state_value);
        var duration_1 = metric_gr.duration;
        gs.info('a3 duration_1 =' + duration_1);
        var duration = new GlideDuration(metric_gr.duration).getNumericValue();
        gs.info('a3 duration =' + duration);
        var duration_2 = metric_gr.duration.getDisplayValue();
        gs.info('a3 duration_2 =' + duration_2);
        //var duration = new GlideDuration(metric_gr.duration);
        //var duration_ms = duration.getNumericValue()
        if (!state_totals[state_value]) {
            state_totals[state_value] = 0;
        }
        state_totals[state_value] += duration;
    }
    gs.info('total =' + state_totals[state_value]);

    if (state_totals['Draft']) { //draft

        current.u_draft_total_duration = new GlideDuration(state_totals['Draft']);
        gs.info('a3 time =' + current.u_draft_total_duration);
    } else {
        current.u_draft_total_duration = new GlideDuration(0);
    }

    if (state_totals['Analysis']) { //analysis

        current.u_analysis_total_duration = new GlideDuration(state_totals['Analysis']);
        gs.info('a3 time_1 =' + current.u_analysis_total_duration);
    } else {
        current.u_analysis_total_duration = new GlideDuration(0);
    }
 
but the script is not calculating the total duration as expected and storing the value as shared in the attachment.
 
 
 
Kindly help me achieve my requirement.
Eg: In the metric instance table there are multiple metric records for a SIR record for different states 
value(state)    duration            ID
draft                    2 minutes       SIR122223
analysis             1-minute          SIR122223
draft                   45 minutes      SIR122223
analysis             12 minutes       SIR122223
draft                   1-day                   SIR122223
 
so the total value for draft should be 1 day 47 minutes and this should be stored in the field which is present in the SIR record.
Can this be achieved with GlideDuration() method?
 
 
0 REPLIES 0