existing metric record update

Priyansh_98
Tera Guru

Hi, 

i have created a metric report to count the reopened cases in system, i created a BR like this

prs1_0-1693383405480.png

and script i have used in my BR

 var metricSysID = 'e26c354c1b683d1477e4c882604bcb8b';
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('definition', metricSysID);
    gr.query();
    var record_count = gr.getRowCount();
    gs.addInfoMessage('record count rule' + record_count);
    if (!gr.next()) {
        updateMetric();
    }
    // // since !mi.next inserts new, this will insert updates.
    // else if (current.active == true && current.operation() == 'update') {
    //     insertMetrics();
    //   //  gs.addInfoMessage('before rule');
    // }


    function insertMetrics() {

        var gr = new GlideRecord('metric_instance');
        //sys id of the metric definition
        var metricSysID = 'e26c354c1b683d1477e4c882604bcb8b';

        gr.initialize();
        gr.definition = metricSysID;
        gr.start = previous.sys_updated_on;
        gr.end = GlideDateTime();
        gr.id = current.sys_id;
        // gr.value = current.state.getDisplayValue();
        gr.value = 'Re-Opened';

        gr.calculation_complete = true;
        gr.insert();
    }

this code is working fine. it is creating a new record every time when the condition met. 

but I received one additional change for its enhancement, where i have to update existing record and also i have to count how many time this this record get updated, and store this count along with the value field.

i tried updating this script like this

   var metricSysID = 'e26c354c1b683d1477e4c882604bcb8b';
    var gr = new GlideRecord('metric_instance');
    gr.addQuery('id', current.sys_id);
    gr.addQuery('definition', metricSysID);
    gr.query();
    var record_count = gr.getRowCount();
    gs.addInfoMessage('record count rule' + record_count);
    
    if (record_count < 1) {
      gs.addInfoMessage('from if ');

        insertMetrics();
    } else {
      gs.addInfoMessage('from else ');

        updateMetric();
    }

    function insertMetrics() {

        var gr = new GlideRecord('metric_instance');
        //sys id of the metric definition
        var metricSysID = 'e26c354c1b683d1477e4c882604bcb8b';

        gr.initialize();
        gr.definition = metricSysID;
        gr.start = previous.sys_updated_on;
        gr.end = GlideDateTime();
        gr.id = current.sys_id;
        // gr.value = current.state.getDisplayValue();
        gr.value = 'Re-Opened';

        gr.calculation_complete = true;
        gr.insert();
    }

    function updateMetric() {
        var ga = new GlideRecord('metric_instance');
        ga.addQuery(current.sys_id);
        ga.query();
        while (ga.next()) {
            ga.start = previous.sys_updated_on;
            ga.end = GlideDateTime();
            //     gr.id = current.sys_id;
            // gr.value = current.state.getDisplayValue();
            ga.value = 'Solution Rejected : '; // + record_count + ' time'
            ga.calculation_complete = true;
            ga.update();
            gs.addInfoMessage('from update ');
        }
    }

 

but this is not updating the existing record in the metric.

can any one help me one this..

0 REPLIES 0